diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-12-24 18:39:31 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-12-24 18:39:31 +0000 |
commit | b65ba8f1d62c2961e520885117056e405056959d (patch) | |
tree | 8b4b48b4e24c2b7fcf81ddc196586efd684a3c66 /crates/ra_editor | |
parent | 67e768466ff2e2611eead0f30b2e9c4083c80c20 (diff) | |
parent | 0fb8894fbe3c2ea9f4be34065c3bd1b2a64f6356 (diff) |
Merge #326
326: resolved #324: remove unnecessary braces in use statement. r=matklad a=gfreezy
Add inspection for unnecessary braces in use statement
Co-authored-by: gfreezy <[email protected]>
Diffstat (limited to 'crates/ra_editor')
-rw-r--r-- | crates/ra_editor/src/lib.rs | 56 |
1 files changed, 54 insertions, 2 deletions
diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs index 7b63b9a88..7a689b0f2 100644 --- a/crates/ra_editor/src/lib.rs +++ b/crates/ra_editor/src/lib.rs | |||
@@ -31,10 +31,17 @@ pub struct HighlightedRange { | |||
31 | pub tag: &'static str, | 31 | pub tag: &'static str, |
32 | } | 32 | } |
33 | 33 | ||
34 | #[derive(Debug, Copy, Clone)] | ||
35 | pub enum Severity { | ||
36 | Error, | ||
37 | WeakWarning, | ||
38 | } | ||
39 | |||
34 | #[derive(Debug)] | 40 | #[derive(Debug)] |
35 | pub struct Diagnostic { | 41 | pub struct Diagnostic { |
36 | pub range: TextRange, | 42 | pub range: TextRange, |
37 | pub msg: String, | 43 | pub msg: String, |
44 | pub severity: Severity, | ||
38 | } | 45 | } |
39 | 46 | ||
40 | #[derive(Debug)] | 47 | #[derive(Debug)] |
@@ -97,13 +104,37 @@ pub fn diagnostics(file: &SourceFileNode) -> Vec<Diagnostic> { | |||
97 | } | 104 | } |
98 | } | 105 | } |
99 | 106 | ||
100 | file.errors() | 107 | let mut errors: Vec<Diagnostic> = file |
108 | .errors() | ||
101 | .into_iter() | 109 | .into_iter() |
102 | .map(|err| Diagnostic { | 110 | .map(|err| Diagnostic { |
103 | range: location_to_range(err.location()), | 111 | range: location_to_range(err.location()), |
104 | msg: format!("Syntax Error: {}", err), | 112 | msg: format!("Syntax Error: {}", err), |
113 | severity: Severity::Error, | ||
105 | }) | 114 | }) |
106 | .collect() | 115 | .collect(); |
116 | |||
117 | let warnings = check_unnecessary_braces_in_use_statement(file); | ||
118 | |||
119 | errors.extend(warnings); | ||
120 | errors | ||
121 | } | ||
122 | |||
123 | fn check_unnecessary_braces_in_use_statement(file: &SourceFileNode) -> Vec<Diagnostic> { | ||
124 | let mut diagnostics = Vec::new(); | ||
125 | for node in file.syntax().descendants() { | ||
126 | if let Some(use_tree_list) = ast::UseTreeList::cast(node) { | ||
127 | if use_tree_list.use_trees().count() <= 1 { | ||
128 | diagnostics.push(Diagnostic { | ||
129 | range: use_tree_list.syntax().range(), | ||
130 | msg: format!("Unnecessary braces in use statement"), | ||
131 | severity: Severity::WeakWarning, | ||
132 | }) | ||
133 | } | ||
134 | } | ||
135 | } | ||
136 | |||
137 | diagnostics | ||
107 | } | 138 | } |
108 | 139 | ||
109 | pub fn syntax_tree(file: &SourceFileNode) -> String { | 140 | pub fn syntax_tree(file: &SourceFileNode) -> String { |
@@ -204,4 +235,25 @@ fn test_foo() {} | |||
204 | 235 | ||
205 | do_check("struct Foo { a: i32, }<|>", "struct Foo <|>{ a: i32, }"); | 236 | do_check("struct Foo { a: i32, }<|>", "struct Foo <|>{ a: i32, }"); |
206 | } | 237 | } |
238 | |||
239 | #[test] | ||
240 | fn test_check_unnecessary_braces_in_use_statement() { | ||
241 | let file = SourceFileNode::parse( | ||
242 | r#" | ||
243 | use a; | ||
244 | use {b}; | ||
245 | use a::{c}; | ||
246 | use a::{c, d::e}; | ||
247 | use a::{c, d::{e}}; | ||
248 | fn main() {} | ||
249 | "#, | ||
250 | ); | ||
251 | let diagnostics = check_unnecessary_braces_in_use_statement(&file); | ||
252 | assert_eq_dbg( | ||
253 | r#"[Diagnostic { range: [12; 15), msg: "Unnecessary braces in use statement", severity: WeakWarning }, | ||
254 | Diagnostic { range: [24; 27), msg: "Unnecessary braces in use statement", severity: WeakWarning }, | ||
255 | Diagnostic { range: [61; 64), msg: "Unnecessary braces in use statement", severity: WeakWarning }]"#, | ||
256 | &diagnostics, | ||
257 | ) | ||
258 | } | ||
207 | } | 259 | } |