mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-03 07:04:49 +00:00
resolved #324: remove unnecessary braces in use statement.
This commit is contained in:
parent
d77520fde3
commit
000aacafda
1 changed files with 53 additions and 2 deletions
|
@ -31,10 +31,17 @@ pub struct HighlightedRange {
|
||||||
pub tag: &'static str,
|
pub tag: &'static str,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Copy, Clone)]
|
||||||
|
pub enum Severity {
|
||||||
|
Error,
|
||||||
|
Warning
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Diagnostic {
|
pub struct Diagnostic {
|
||||||
pub range: TextRange,
|
pub range: TextRange,
|
||||||
pub msg: String,
|
pub msg: String,
|
||||||
|
pub severity: Severity,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -97,13 +104,36 @@ pub fn diagnostics(file: &SourceFileNode) -> Vec<Diagnostic> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
file.errors()
|
let mut errors: Vec<Diagnostic> = file.errors()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|err| Diagnostic {
|
.map(|err| Diagnostic {
|
||||||
range: location_to_range(err.location()),
|
range: location_to_range(err.location()),
|
||||||
msg: format!("Syntax Error: {}", err),
|
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<Diagnostic> {
|
||||||
|
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 {
|
pub fn syntax_tree(file: &SourceFileNode) -> String {
|
||||||
|
@ -204,4 +234,25 @@ fn test_foo() {}
|
||||||
|
|
||||||
do_check("struct Foo { a: i32, }<|>", "struct Foo <|>{ a: i32, }");
|
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,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue