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') 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_analysis/src/imp.rs | 5 ++++- crates/ra_analysis/src/lib.rs | 2 ++ crates/ra_analysis/tests/tests.rs | 3 ++- crates/ra_editor/src/lib.rs | 19 +++++++++++-------- crates/ra_lsp_server/src/main_loop/handlers.rs | 15 +++++++++++++-- 5 files changed, 32 insertions(+), 12 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index b01382808..e054227a9 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -3,7 +3,7 @@ use std::{ sync::Arc, }; -use ra_editor::{self, find_node_at_offset, FileSymbol, LineIndex, LocalEdit}; +use ra_editor::{self, find_node_at_offset, FileSymbol, LineIndex, LocalEdit, Severity}; use ra_syntax::{ ast::{self, ArgListOwner, Expr, NameOwner}, AstNode, SourceFileNode, @@ -364,6 +364,7 @@ impl AnalysisImpl { .map(|d| Diagnostic { range: d.range, message: d.msg, + severity: d.severity, fix: None, }) .collect::>(); @@ -385,6 +386,7 @@ impl AnalysisImpl { Diagnostic { range: name_node.range(), message: "unresolved module".to_string(), + severity: Some(Severity::Error), fix: Some(fix), } } @@ -407,6 +409,7 @@ impl AnalysisImpl { Diagnostic { range: name_node.range(), message: "can't declare module at this location".to_string(), + severity: Some(Severity::Error), fix: Some(fix), } } diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index 85df9c089..8ab6334a7 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -34,6 +34,7 @@ pub use crate::{ }; pub use ra_editor::{ FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable, RunnableKind, StructureNode, + Severity }; pub use hir::FnSignatureInfo; @@ -198,6 +199,7 @@ pub struct Diagnostic { pub message: String, pub range: TextRange, pub fix: Option, + pub severity: Option, } #[derive(Debug)] diff --git a/crates/ra_analysis/tests/tests.rs b/crates/ra_analysis/tests/tests.rs index 938ca797a..2313e35f5 100644 --- a/crates/ra_analysis/tests/tests.rs +++ b/crates/ra_analysis/tests/tests.rs @@ -82,7 +82,8 @@ fn test_unresolved_module_diagnostic() { label: "create module", source_file_edits: [], file_system_edits: [CreateFile { source_root: SourceRootId(0), path: "foo.rs" }], - cursor_position: None }) }]"#, + cursor_position: None }), + severity: Some(Error) }]"#, &diagnostics, ); } 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 Result> { .collect(); Ok(res) } + +fn to_diagnostic_severity(severity: Severity) -> DiagnosticSeverity { + use ra_analysis::Severity::*; + + match severity { + Error => DiagnosticSeverity::Error, + Warning => DiagnosticSeverity::Warning, + Information => DiagnosticSeverity::Information, + Hint => DiagnosticSeverity::Hint, + } +} -- cgit v1.2.3 From 17b35a7f7dc5054f8d632cd4d3fc2fcb26879819 Mon Sep 17 00:00:00 2001 From: gfreezy Date: Mon, 24 Dec 2018 00:40:36 +0800 Subject: remove unnecessary braces in use statments --- crates/ra_analysis/src/lib.rs | 4 +--- crates/ra_analysis/src/mock_analysis.rs | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index 8ab6334a7..a213fd60f 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -29,9 +29,7 @@ use crate::{ symbol_index::SymbolIndex, }; -pub use crate::{ - completion::{CompletionItem, CompletionItemKind, InsertText}, -}; +pub use crate::completion::{CompletionItem, CompletionItemKind, InsertText}; pub use ra_editor::{ FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable, RunnableKind, StructureNode, Severity diff --git a/crates/ra_analysis/src/mock_analysis.rs b/crates/ra_analysis/src/mock_analysis.rs index 7cbdfb953..5ce2aa2b4 100644 --- a/crates/ra_analysis/src/mock_analysis.rs +++ b/crates/ra_analysis/src/mock_analysis.rs @@ -1,6 +1,6 @@ use std::sync::Arc; -use relative_path::{RelativePathBuf}; +use relative_path::RelativePathBuf; use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER}; use ra_db::mock::FileMap; -- cgit v1.2.3 From 70df097c89ee45e4e0709c21b8aeee2e84e09fc4 Mon Sep 17 00:00:00 2001 From: gfreezy Date: Mon, 24 Dec 2018 22:48:46 +0800 Subject: keep severity to Error & WeakWarning --- crates/ra_analysis/src/imp.rs | 2 +- crates/ra_editor/src/lib.rs | 16 +++++++--------- 2 files changed, 8 insertions(+), 10 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index e054227a9..e8d6acc17 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -364,7 +364,7 @@ impl AnalysisImpl { .map(|d| Diagnostic { range: d.range, message: d.msg, - severity: d.severity, + severity: Some(d.severity), fix: None, }) .collect::>(); 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 Date: Mon, 24 Dec 2018 23:00:18 +0800 Subject: remove option from Diagnostic --- crates/ra_analysis/src/imp.rs | 6 +++--- crates/ra_analysis/src/lib.rs | 2 +- crates/ra_lsp_server/src/main_loop.rs | 4 ++-- crates/ra_lsp_server/src/main_loop/handlers.rs | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index e8d6acc17..00cdf7eff 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -364,7 +364,7 @@ impl AnalysisImpl { .map(|d| Diagnostic { range: d.range, message: d.msg, - severity: Some(d.severity), + severity: d.severity, fix: None, }) .collect::>(); @@ -386,7 +386,7 @@ impl AnalysisImpl { Diagnostic { range: name_node.range(), message: "unresolved module".to_string(), - severity: Some(Severity::Error), + severity: Severity::Error, fix: Some(fix), } } @@ -409,7 +409,7 @@ impl AnalysisImpl { Diagnostic { range: name_node.range(), message: "can't declare module at this location".to_string(), - severity: Some(Severity::Error), + severity: Severity::Error, fix: Some(fix), } } diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index a213fd60f..fbb19ed6b 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -197,7 +197,7 @@ pub struct Diagnostic { pub message: String, pub range: TextRange, pub fix: Option, - pub severity: Option, + pub severity: Severity, } #[derive(Debug)] diff --git a/crates/ra_lsp_server/src/main_loop.rs b/crates/ra_lsp_server/src/main_loop.rs index 565ec92af..c0c414db0 100644 --- a/crates/ra_lsp_server/src/main_loop.rs +++ b/crates/ra_lsp_server/src/main_loop.rs @@ -13,7 +13,7 @@ use gen_lsp_server::{ }; use languageserver_types::NumberOrString; use ra_analysis::{Canceled, FileId, LibraryData}; -use ra_vfs::{VfsTask}; +use ra_vfs::VfsTask; use rayon; use threadpool::ThreadPool; use rustc_hash::FxHashSet; @@ -23,7 +23,7 @@ use failure_derive::Fail; use crate::{ main_loop::subscriptions::Subscriptions, - project_model::{workspace_loader}, + project_model::workspace_loader, req, server_world::{ServerWorld, ServerWorldState}, Result, diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 8e859e8d4..658d169cd 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -650,7 +650,7 @@ pub fn publish_diagnostics( .into_iter() .map(|d| Diagnostic { range: d.range.conv_with(&line_index), - severity: d.severity.map(to_diagnostic_severity), + severity: Some(to_diagnostic_severity(d.severity)), code: None, source: Some("rust-analyzer".to_string()), message: d.message, -- cgit v1.2.3 From 0fb8894fbe3c2ea9f4be34065c3bd1b2a64f6356 Mon Sep 17 00:00:00 2001 From: gfreezy Date: Mon, 24 Dec 2018 23:01:16 +0800 Subject: fix tests --- crates/ra_analysis/tests/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates') diff --git a/crates/ra_analysis/tests/tests.rs b/crates/ra_analysis/tests/tests.rs index 2313e35f5..210fa2a13 100644 --- a/crates/ra_analysis/tests/tests.rs +++ b/crates/ra_analysis/tests/tests.rs @@ -83,7 +83,7 @@ fn test_unresolved_module_diagnostic() { source_file_edits: [], file_system_edits: [CreateFile { source_root: SourceRootId(0), path: "foo.rs" }], cursor_position: None }), - severity: Some(Error) }]"#, + severity: Error }]"#, &diagnostics, ); } -- cgit v1.2.3