Skip to content

Commit 72c6fc3

Browse files
committed
Fix add visibility false-positive
1 parent b3665fc commit 72c6fc3

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

crates/ra_assists/src/handlers/change_visibility.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ use ra_syntax::{
22
ast::{self, NameOwner, VisibilityOwner},
33
AstNode,
44
SyntaxKind::{
5-
ATTR, COMMENT, CONST_DEF, ENUM_DEF, FN_DEF, IDENT, MODULE, STRUCT_DEF, TRAIT_DEF,
6-
VISIBILITY, WHITESPACE,
5+
ATTR, COMMENT, CONST_DEF, ENUM_DEF, FN_DEF, MODULE, STRUCT_DEF, TRAIT_DEF, VISIBILITY,
6+
WHITESPACE,
77
},
88
SyntaxNode, TextUnit, T,
99
};
1010

1111
use crate::{Assist, AssistCtx, AssistId};
12+
use test_utils::tested_by;
1213

1314
// Assist: change_visibility
1415
//
@@ -47,13 +48,16 @@ fn add_vis(ctx: AssistCtx) -> Option<Assist> {
4748
}
4849
(vis_offset(&parent), keyword.text_range())
4950
} else {
50-
let ident = ctx.token_at_offset().find(|leaf| leaf.kind() == IDENT)?;
51-
let field = ident.parent().ancestors().find_map(ast::RecordFieldDef::cast)?;
52-
if field.name()?.syntax().text_range() != ident.text_range() && field.visibility().is_some()
53-
{
51+
let field_name: ast::Name = ctx.find_node_at_offset()?;
52+
let field = field_name.syntax().ancestors().find_map(ast::RecordFieldDef::cast)?;
53+
if field.name()? != field_name {
54+
tested_by!(change_visibility_field_false_positive);
5455
return None;
5556
}
56-
(vis_offset(field.syntax()), ident.text_range())
57+
if field.visibility().is_some() {
58+
return None;
59+
}
60+
(vis_offset(field.syntax()), field_name.syntax().text_range())
5761
};
5862

5963
ctx.add_assist(AssistId("change_visibility"), "Change visibility to pub(crate)", |edit| {
@@ -98,8 +102,11 @@ fn change_vis(ctx: AssistCtx, vis: ast::Visibility) -> Option<Assist> {
98102

99103
#[cfg(test)]
100104
mod tests {
105+
use test_utils::covers;
106+
107+
use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_target};
108+
101109
use super::*;
102-
use crate::helpers::{check_assist, check_assist_target};
103110

104111
#[test]
105112
fn change_visibility_adds_pub_crate_to_items() {
@@ -120,8 +127,17 @@ mod tests {
120127
fn change_visibility_works_with_struct_fields() {
121128
check_assist(
122129
change_visibility,
123-
"struct S { <|>field: u32 }",
124-
"struct S { <|>pub(crate) field: u32 }",
130+
r"struct S { <|>field: u32 }",
131+
r"struct S { <|>pub(crate) field: u32 }",
132+
)
133+
}
134+
135+
#[test]
136+
fn change_visibility_field_false_positive() {
137+
covers!(change_visibility_field_false_positive);
138+
check_assist_not_applicable(
139+
change_visibility,
140+
r"struct S { field: [(); { let <|>x = ();}] }",
125141
)
126142
}
127143

@@ -144,15 +160,15 @@ mod tests {
144160
fn change_visibility_handles_comment_attrs() {
145161
check_assist(
146162
change_visibility,
147-
"
163+
r"
148164
/// docs
149165
150166
// comments
151167
152168
#[derive(Debug)]
153169
<|>struct Foo;
154170
",
155-
"
171+
r"
156172
/// docs
157173
158174
// comments

crates/ra_assists/src/marks.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ test_utils::marks![
77
not_applicable_outside_of_bind_pat
88
test_not_inline_mut_variable
99
test_not_applicable_if_variable_unused
10+
change_visibility_field_false_positive
1011
];

0 commit comments

Comments
 (0)