braces around {self} in UseTree are not unnecessary · rust-lang/rust@dd16cbc (original) (raw)
File tree
2 files changed
lines changed
- src/tools/rust-analyzer/crates
2 files changed
lines changed
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -776,6 +776,40 @@ mod z { | ||
776 | 776 | ); |
777 | 777 | } |
778 | 778 | |
779 | +#[test] | |
780 | +fn remove_unused_fixes_nested_self() { | |
781 | +check_assist( | |
782 | + remove_unused_imports, | |
783 | +r#" | |
784 | +mod inner { | |
785 | + pub struct X(); | |
786 | + pub struct Y(); | |
787 | +} | |
788 | + | |
789 | +mod z { | |
790 | + use super::inner::{self, X}$0; | |
791 | + | |
792 | + fn f() { | |
793 | + let y = inner::Y(); | |
794 | + } | |
795 | +} | |
796 | +"#, | |
797 | +r#"mod inner { | |
798 | + pub struct X(); | |
799 | + pub struct Y(); | |
800 | +} | |
801 | + | |
802 | +mod z { | |
803 | + use super::inner::{self}; | |
804 | + | |
805 | + fn f() { | |
806 | + let y = inner::Y(); | |
807 | + } | |
808 | +} | |
809 | +"#, | |
810 | +); | |
811 | +} | |
812 | + | |
779 | 813 | #[test] |
780 | 814 | fn dont_remove_used_glob() { |
781 | 815 | check_assist_not_applicable( |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -378,9 +378,26 @@ impl ast::UseTreeList { | ||
378 | 378 | |
379 | 379 | /// Remove the unnecessary braces in current `UseTreeList` |
380 | 380 | pub fn remove_unnecessary_braces(mut self) { |
381 | +// Returns true iff there is a single subtree and it is not the self keyword. The braces in | |
382 | +// `use x::{self};` are necessary and so we should not remove them. | |
383 | +let has_single_subtree_that_is_not_self = |u: &ast::UseTreeList | |
384 | +if let Some((single_subtree,)) = u.use_trees().collect_tuple() { | |
385 | +// We have a single subtree, check whether it is self. | |
386 | + | |
387 | +let is_self = single_subtree.path().as_ref().map_or(false, |path | |
388 | + path.segment().and_then(|seg | |
389 | + && path.qualifier().is_none() | |
390 | +}); | |
391 | + | |
392 | + !is_self | |
393 | +} else { | |
394 | +// Not a single subtree | |
395 | +false | |
396 | +} | |
397 | +}; | |
398 | + | |
381 | 399 | let remove_brace_in_use_tree_list = |u: &ast::UseTreeList |
382 | -let use_tree_count = u.use_trees().count(); | |
383 | -if use_tree_count == 1 { | |
400 | +if has_single_subtree_that_is_not_self(u) { | |
384 | 401 | if let Some(a) = u.l_curly_token() { |
385 | 402 | ted::remove(a) |
386 | 403 | } |