Skip to content

Commit c4e88e9

Browse files
bors[bot]lnicola
andauthored
Merge #10555
10555: minor: Hide private methods in `generate_delegate_methods` r=lnicola a=lnicola Fixes #10553 Co-authored-by: Laurențiu Nicola <[email protected]>
2 parents 6c7526d + cd0c45f commit c4e88e9

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

crates/ide_assists/src/handlers/generate_delegate_methods.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use hir::{self, HasCrate, HasSource};
2-
use syntax::ast::{self, make, AstNode, HasGenericParams, HasName, HasVisibility};
1+
use hir::{self, HasCrate, HasSource, HasVisibility};
2+
use syntax::ast::{self, make, AstNode, HasGenericParams, HasName, HasVisibility as _};
33

44
use crate::{
55
utils::{convert_param_list_to_arg_list, find_struct_impl, render_snippet, Cursor},
@@ -45,6 +45,7 @@ use syntax::ast::edit::AstNodeEdit;
4545
pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
4646
let strukt = ctx.find_node_at_offset::<ast::Struct>()?;
4747
let strukt_name = strukt.name()?;
48+
let current_module = ctx.sema.scope(strukt.syntax()).module()?;
4849

4950
let (field_name, field_ty) = match ctx.find_node_at_offset::<ast::RecordField>() {
5051
Some(field) => {
@@ -66,7 +67,7 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext)
6667
let mut methods = vec![];
6768
sema_field_ty.iterate_assoc_items(ctx.db(), krate, |item| {
6869
if let hir::AssocItem::Function(f) = item {
69-
if f.self_param(ctx.db()).is_some() {
70+
if f.self_param(ctx.db()).is_some() && f.is_visible_from(ctx.db(), current_module) {
7071
methods.push(f)
7172
}
7273
}
@@ -170,7 +171,7 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext)
170171

171172
#[cfg(test)]
172173
mod tests {
173-
use crate::tests::check_assist;
174+
use crate::tests::{check_assist, check_assist_not_applicable};
174175

175176
use super::*;
176177

@@ -311,4 +312,24 @@ impl<T> Person<T> {
311312
}"#,
312313
);
313314
}
315+
316+
#[test]
317+
fn test_generate_delegate_visibility() {
318+
check_assist_not_applicable(
319+
generate_delegate_methods,
320+
r#"
321+
mod m {
322+
pub struct Age(u8);
323+
impl Age {
324+
fn age(&self) -> u8 {
325+
self.0
326+
}
327+
}
328+
}
329+
330+
struct Person {
331+
ag$0e: m::Age,
332+
}"#,
333+
)
334+
}
314335
}

0 commit comments

Comments
 (0)