aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2018-12-24 18:39:31 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2018-12-24 18:39:31 +0000
commitb65ba8f1d62c2961e520885117056e405056959d (patch)
tree8b4b48b4e24c2b7fcf81ddc196586efd684a3c66
parent67e768466ff2e2611eead0f30b2e9c4083c80c20 (diff)
parent0fb8894fbe3c2ea9f4be34065c3bd1b2a64f6356 (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]>
-rw-r--r--crates/ra_analysis/src/imp.rs5
-rw-r--r--crates/ra_analysis/src/lib.rs6
-rw-r--r--crates/ra_analysis/src/mock_analysis.rs2
-rw-r--r--crates/ra_analysis/tests/tests.rs3
-rw-r--r--crates/ra_editor/src/lib.rs56
-rw-r--r--crates/ra_lsp_server/src/main_loop.rs4
-rw-r--r--crates/ra_lsp_server/src/main_loop/handlers.rs15
7 files changed, 79 insertions, 12 deletions
diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs
index 40996bfd7..a547c5a20 100644
--- a/crates/ra_analysis/src/imp.rs
+++ b/crates/ra_analysis/src/imp.rs
@@ -3,7 +3,7 @@ use std::{
3 sync::Arc, 3 sync::Arc,
4}; 4};
5 5
6use ra_editor::{self, find_node_at_offset, FileSymbol, LineIndex, LocalEdit}; 6use ra_editor::{self, find_node_at_offset, FileSymbol, LineIndex, LocalEdit, Severity};
7use ra_syntax::{ 7use ra_syntax::{
8 ast::{self, ArgListOwner, Expr, NameOwner, FnDef}, 8 ast::{self, ArgListOwner, Expr, NameOwner, FnDef},
9 algo::find_covering_node, 9 algo::find_covering_node,
@@ -365,6 +365,7 @@ impl AnalysisImpl {
365 .map(|d| Diagnostic { 365 .map(|d| Diagnostic {
366 range: d.range, 366 range: d.range,
367 message: d.msg, 367 message: d.msg,
368 severity: d.severity,
368 fix: None, 369 fix: None,
369 }) 370 })
370 .collect::<Vec<_>>(); 371 .collect::<Vec<_>>();
@@ -386,6 +387,7 @@ impl AnalysisImpl {
386 Diagnostic { 387 Diagnostic {
387 range: name_node.range(), 388 range: name_node.range(),
388 message: "unresolved module".to_string(), 389 message: "unresolved module".to_string(),
390 severity: Severity::Error,
389 fix: Some(fix), 391 fix: Some(fix),
390 } 392 }
391 } 393 }
@@ -408,6 +410,7 @@ impl AnalysisImpl {
408 Diagnostic { 410 Diagnostic {
409 range: name_node.range(), 411 range: name_node.range(),
410 message: "can't declare module at this location".to_string(), 412 message: "can't declare module at this location".to_string(),
413 severity: Severity::Error,
411 fix: Some(fix), 414 fix: Some(fix),
412 } 415 }
413 } 416 }
diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs
index 830898140..a029f66b4 100644
--- a/crates/ra_analysis/src/lib.rs
+++ b/crates/ra_analysis/src/lib.rs
@@ -29,11 +29,10 @@ use crate::{
29 symbol_index::SymbolIndex, 29 symbol_index::SymbolIndex,
30}; 30};
31 31
32pub use crate::{ 32pub use crate::completion::{CompletionItem, CompletionItemKind, InsertText};
33 completion::{CompletionItem, CompletionItemKind, InsertText},
34};
35pub use ra_editor::{ 33pub use ra_editor::{
36 FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable, RunnableKind, StructureNode, 34 FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable, RunnableKind, StructureNode,
35 Severity
37}; 36};
38pub use hir::FnSignatureInfo; 37pub use hir::FnSignatureInfo;
39 38
@@ -198,6 +197,7 @@ pub struct Diagnostic {
198 pub message: String, 197 pub message: String,
199 pub range: TextRange, 198 pub range: TextRange,
200 pub fix: Option<SourceChange>, 199 pub fix: Option<SourceChange>,
200 pub severity: Severity,
201} 201}
202 202
203#[derive(Debug)] 203#[derive(Debug)]
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 @@
1use std::sync::Arc; 1use std::sync::Arc;
2 2
3use relative_path::{RelativePathBuf}; 3use relative_path::RelativePathBuf;
4use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER}; 4use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER};
5use ra_db::mock::FileMap; 5use ra_db::mock::FileMap;
6 6
diff --git a/crates/ra_analysis/tests/tests.rs b/crates/ra_analysis/tests/tests.rs
index 938ca797a..210fa2a13 100644
--- a/crates/ra_analysis/tests/tests.rs
+++ b/crates/ra_analysis/tests/tests.rs
@@ -82,7 +82,8 @@ fn test_unresolved_module_diagnostic() {
82 label: "create module", 82 label: "create module",
83 source_file_edits: [], 83 source_file_edits: [],
84 file_system_edits: [CreateFile { source_root: SourceRootId(0), path: "foo.rs" }], 84 file_system_edits: [CreateFile { source_root: SourceRootId(0), path: "foo.rs" }],
85 cursor_position: None }) }]"#, 85 cursor_position: None }),
86 severity: Error }]"#,
86 &diagnostics, 87 &diagnostics,
87 ); 88 );
88} 89}
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)]
35pub enum Severity {
36 Error,
37 WeakWarning,
38}
39
34#[derive(Debug)] 40#[derive(Debug)]
35pub struct Diagnostic { 41pub 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
123fn 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
109pub fn syntax_tree(file: &SourceFileNode) -> String { 140pub 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#"
243use a;
244use {b};
245use a::{c};
246use a::{c, d::e};
247use a::{c, d::{e}};
248fn 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}
diff --git a/crates/ra_lsp_server/src/main_loop.rs b/crates/ra_lsp_server/src/main_loop.rs
index a5a2b5eec..60f13267c 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::{
13}; 13};
14use languageserver_types::NumberOrString; 14use languageserver_types::NumberOrString;
15use ra_analysis::{Canceled, FileId, LibraryData}; 15use ra_analysis::{Canceled, FileId, LibraryData};
16use ra_vfs::{VfsTask}; 16use ra_vfs::VfsTask;
17use rayon; 17use rayon;
18use threadpool::ThreadPool; 18use threadpool::ThreadPool;
19use rustc_hash::FxHashSet; 19use rustc_hash::FxHashSet;
@@ -23,7 +23,7 @@ use failure_derive::Fail;
23 23
24use crate::{ 24use crate::{
25 main_loop::subscriptions::Subscriptions, 25 main_loop::subscriptions::Subscriptions,
26 project_model::{workspace_loader}, 26 project_model::workspace_loader,
27 req, 27 req,
28 server_world::{ServerWorld, ServerWorldState}, 28 server_world::{ServerWorld, ServerWorldState},
29 Result, 29 Result,
diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs
index 252d1ba3e..658d169cd 100644
--- a/crates/ra_lsp_server/src/main_loop/handlers.rs
+++ b/crates/ra_lsp_server/src/main_loop/handlers.rs
@@ -8,7 +8,7 @@ use languageserver_types::{
8 PrepareRenameResponse, RenameParams, SymbolInformation, TextDocumentIdentifier, TextEdit, 8 PrepareRenameResponse, RenameParams, SymbolInformation, TextDocumentIdentifier, TextEdit,
9 WorkspaceEdit, ParameterInformation, ParameterLabel, SignatureInformation, Hover, HoverContents, 9 WorkspaceEdit, ParameterInformation, ParameterLabel, SignatureInformation, Hover, HoverContents,
10}; 10};
11use ra_analysis::{FileId, FoldKind, Query, RunnableKind, FilePosition}; 11use ra_analysis::{FileId, FoldKind, Query, RunnableKind, FilePosition, Severity};
12use ra_syntax::{TextUnit, text_utils::intersect}; 12use ra_syntax::{TextUnit, text_utils::intersect};
13use ra_text_edit::text_utils::contains_offset_nonstrict; 13use ra_text_edit::text_utils::contains_offset_nonstrict;
14use rustc_hash::FxHashMap; 14use rustc_hash::FxHashMap;
@@ -650,7 +650,7 @@ pub fn publish_diagnostics(
650 .into_iter() 650 .into_iter()
651 .map(|d| Diagnostic { 651 .map(|d| Diagnostic {
652 range: d.range.conv_with(&line_index), 652 range: d.range.conv_with(&line_index),
653 severity: Some(DiagnosticSeverity::Error), 653 severity: Some(to_diagnostic_severity(d.severity)),
654 code: None, 654 code: None,
655 source: Some("rust-analyzer".to_string()), 655 source: Some("rust-analyzer".to_string()),
656 message: d.message, 656 message: d.message,
@@ -684,3 +684,14 @@ fn highlight(world: &ServerWorld, file_id: FileId) -> Result<Vec<Decoration>> {
684 .collect(); 684 .collect();
685 Ok(res) 685 Ok(res)
686} 686}
687
688fn to_diagnostic_severity(severity: Severity) -> DiagnosticSeverity {
689 use ra_analysis::Severity::*;
690
691 match severity {
692 Error => DiagnosticSeverity::Error,
693 Warning => DiagnosticSeverity::Warning,
694 Information => DiagnosticSeverity::Information,
695 Hint => DiagnosticSeverity::Hint,
696 }
697}