From 000aacafda209826b9b5aac86d175152d488f05b Mon Sep 17 00:00:00 2001 From: gfreezy Date: Sun, 23 Dec 2018 23:50:11 +0800 Subject: resolved #324: remove unnecessary braces in use statement. --- crates/ra_editor/src/lib.rs | 55 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs index 36cabed25..48676f2e8 100644 --- a/crates/ra_editor/src/lib.rs +++ b/crates/ra_editor/src/lib.rs @@ -31,10 +31,17 @@ pub struct HighlightedRange { pub tag: &'static str, } +#[derive(Debug, Copy, Clone)] +pub enum Severity { + Error, + Warning +} + #[derive(Debug)] pub struct Diagnostic { pub range: TextRange, pub msg: String, + pub severity: Severity, } #[derive(Debug)] @@ -97,13 +104,36 @@ pub fn diagnostics(file: &SourceFileNode) -> Vec { } } - file.errors() + let mut errors: Vec = file.errors() .into_iter() .map(|err| Diagnostic { range: location_to_range(err.location()), msg: format!("Syntax Error: {}", err), + severity: Severity::Error, }) - .collect() + .collect(); + + let warnings = check_unnecessary_braces_in_use_statement(file); + + errors.extend(warnings); + errors +} + +fn check_unnecessary_braces_in_use_statement(file: &SourceFileNode) -> Vec { + let mut diagnostics = Vec::new(); + for node in file.syntax().descendants() { + if let Some(use_tree_list) = ast::UseTreeList::cast(node) { + if use_tree_list.use_trees().count() <= 1 { + diagnostics.push(Diagnostic { + range: use_tree_list.syntax().range(), + msg: format!("Unnecessary braces in use statement"), + severity: Severity::Warning, + }) + } + } + } + + diagnostics } pub fn syntax_tree(file: &SourceFileNode) -> String { @@ -204,4 +234,25 @@ fn test_foo() {} do_check("struct Foo { a: i32, }<|>", "struct Foo <|>{ a: i32, }"); } + + #[test] + fn test_check_unnecessary_braces_in_use_statement() { + let file = SourceFileNode::parse( + r#" +use a; +use {b}; +use a::{c}; +use a::{c, d::e}; +use a::{c, d::{e}}; +fn main() {} +"#, + ); + let diagnostics = check_unnecessary_braces_in_use_statement(&file); + assert_eq_dbg( + r#"[Diagnostic { range: [12; 15), msg: "Unnecessary braces in use statement", severity: Warning }, + Diagnostic { range: [24; 27), msg: "Unnecessary braces in use statement", severity: Warning }, + Diagnostic { range: [61; 64), msg: "Unnecessary braces in use statement", severity: Warning }]"#, + &diagnostics, + ) + } } -- cgit v1.2.3 From 346638c8098fefd0b6fa3cf81fbdf22ebfaab9be Mon Sep 17 00:00:00 2001 From: gfreezy Date: Mon, 24 Dec 2018 00:39:33 +0800 Subject: add serverity to vscode diagnostics --- crates/ra_editor/src/lib.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs index 48676f2e8..399bb8fe8 100644 --- a/crates/ra_editor/src/lib.rs +++ b/crates/ra_editor/src/lib.rs @@ -34,14 +34,16 @@ pub struct HighlightedRange { #[derive(Debug, Copy, Clone)] pub enum Severity { Error, - Warning + Warning, + Information, + Hint, } #[derive(Debug)] pub struct Diagnostic { pub range: TextRange, pub msg: String, - pub severity: Severity, + pub severity: Option, } #[derive(Debug)] @@ -104,12 +106,13 @@ pub fn diagnostics(file: &SourceFileNode) -> Vec { } } - let mut errors: Vec = file.errors() + let mut errors: Vec = file + .errors() .into_iter() .map(|err| Diagnostic { range: location_to_range(err.location()), msg: format!("Syntax Error: {}", err), - severity: Severity::Error, + severity: Some(Severity::Error), }) .collect(); @@ -127,7 +130,7 @@ fn check_unnecessary_braces_in_use_statement(file: &SourceFileNode) -> Vec Date: Mon, 24 Dec 2018 22:48:46 +0800 Subject: keep severity to Error & WeakWarning --- crates/ra_editor/src/lib.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs index 399bb8fe8..75e4a5f32 100644 --- a/crates/ra_editor/src/lib.rs +++ b/crates/ra_editor/src/lib.rs @@ -34,16 +34,14 @@ pub struct HighlightedRange { #[derive(Debug, Copy, Clone)] pub enum Severity { Error, - Warning, - Information, - Hint, + WeakWarning, } #[derive(Debug)] pub struct Diagnostic { pub range: TextRange, pub msg: String, - pub severity: Option, + pub severity: Severity, } #[derive(Debug)] @@ -112,7 +110,7 @@ pub fn diagnostics(file: &SourceFileNode) -> Vec { .map(|err| Diagnostic { range: location_to_range(err.location()), msg: format!("Syntax Error: {}", err), - severity: Some(Severity::Error), + severity: Severity::Error, }) .collect(); @@ -130,7 +128,7 @@ fn check_unnecessary_braces_in_use_statement(file: &SourceFileNode) -> Vec