3564: Better handling of a few kinds of cargo/clippy diagnostics r=matklad a=kiljacken

This was initially supposed to just be a fix for #3433, but I caught a few things that ended up being useful as well.

This PR primarily makes us handle multi-edit fix suggestions properly. Instead of just applying the first fix we apply all the parts of the fix in a single action.

Second up, this PR handles diagnostics with multiple primary spans, f.x. the unused import diagnostic from rustc:
![image](https://user-images.githubusercontent.com/209321/76531793-03269480-6476-11ea-9180-41c0ea705553.png)

The LSP doesn't handle this too well, as it only support a single complete range for each diagnostic, so we get duplicate messages in the problem panel of VSCode:
![image](https://user-images.githubusercontent.com/209321/76531901-29e4cb00-6476-11ea-9746-cd57f8974b85.png)

However, I feel like the improved visual aspect in-editor outweighs the duplication in the problem panel. I'm open to not including the second commit if anybody really doesn't like the idea of duplicate diagnostics in the problem pane.

Fixes #3433
Fixes #3257

Co-authored-by: Emil Lauridsen <mine809@gmail.com>
This commit is contained in:
bors[bot] 2020-03-12 14:46:42 +00:00 committed by GitHub
commit 7bbdca6182
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 729 additions and 465 deletions

View file

@ -8,6 +8,7 @@ use lsp_types::{
Location, NumberOrString, Position, Range, TextEdit, Url, WorkspaceEdit, Location, NumberOrString, Position, Range, TextEdit, Url, WorkspaceEdit,
}; };
use std::{ use std::{
collections::HashMap,
fmt::Write, fmt::Write,
path::{Component, Path, PathBuf, Prefix}, path::{Component, Path, PathBuf, Prefix},
str::FromStr, str::FromStr,
@ -126,44 +127,34 @@ fn map_rust_child_diagnostic(
rd: &RustDiagnostic, rd: &RustDiagnostic,
workspace_root: &PathBuf, workspace_root: &PathBuf,
) -> MappedRustChildDiagnostic { ) -> MappedRustChildDiagnostic {
let span: &DiagnosticSpan = match rd.spans.iter().find(|s| s.is_primary) { let spans: Vec<&DiagnosticSpan> = rd.spans.iter().filter(|s| s.is_primary).collect();
Some(span) => span, if spans.is_empty() {
None => { // `rustc` uses these spanless children as a way to print multi-line
// `rustc` uses these spanless children as a way to print multi-line // messages
// messages return MappedRustChildDiagnostic::MessageLine(rd.message.clone());
return MappedRustChildDiagnostic::MessageLine(rd.message.clone()); }
let mut edit_map: HashMap<Url, Vec<TextEdit>> = HashMap::new();
for &span in &spans {
if let Some(suggested_replacement) = &span.suggested_replacement {
let location = map_span_to_location(span, workspace_root);
let edit = TextEdit::new(location.range, suggested_replacement.clone());
edit_map.entry(location.uri).or_default().push(edit);
} }
}; }
// If we have a primary span use its location, otherwise use the parent
let location = map_span_to_location(&span, workspace_root);
if let Some(suggested_replacement) = &span.suggested_replacement {
// Include our replacement in the title unless it's empty
let title = if !suggested_replacement.is_empty() {
format!("{}: '{}'", rd.message, suggested_replacement)
} else {
rd.message.clone()
};
let edit = {
let edits = vec![TextEdit::new(location.range, suggested_replacement.clone())];
let mut edit_map = std::collections::HashMap::new();
edit_map.insert(location.uri, edits);
WorkspaceEdit::new(edit_map)
};
if !edit_map.is_empty() {
MappedRustChildDiagnostic::SuggestedFix(CodeAction { MappedRustChildDiagnostic::SuggestedFix(CodeAction {
title, title: rd.message.clone(),
kind: Some("quickfix".to_string()), kind: Some("quickfix".to_string()),
diagnostics: None, diagnostics: None,
edit: Some(edit), edit: Some(WorkspaceEdit::new(edit_map)),
command: None, command: None,
is_preferred: None, is_preferred: None,
}) })
} else { } else {
MappedRustChildDiagnostic::Related(DiagnosticRelatedInformation { MappedRustChildDiagnostic::Related(DiagnosticRelatedInformation {
location, location: map_span_to_location(spans[0], workspace_root),
message: rd.message.clone(), message: rd.message.clone(),
}) })
} }
@ -189,13 +180,13 @@ pub(crate) struct MappedRustDiagnostic {
pub(crate) fn map_rust_diagnostic_to_lsp( pub(crate) fn map_rust_diagnostic_to_lsp(
rd: &RustDiagnostic, rd: &RustDiagnostic,
workspace_root: &PathBuf, workspace_root: &PathBuf,
) -> Option<MappedRustDiagnostic> { ) -> Vec<MappedRustDiagnostic> {
let primary_span = rd.spans.iter().find(|s| s.is_primary)?; let primary_spans: Vec<&DiagnosticSpan> = rd.spans.iter().filter(|s| s.is_primary).collect();
if primary_spans.is_empty() {
let location = map_span_to_location(&primary_span, workspace_root); return vec![];
}
let severity = map_level_to_severity(rd.level); let severity = map_level_to_severity(rd.level);
let mut primary_span_label = primary_span.label.as_ref();
let mut source = String::from("rustc"); let mut source = String::from("rustc");
let mut code = rd.code.as_ref().map(|c| c.code.clone()); let mut code = rd.code.as_ref().map(|c| c.code.clone());
@ -208,19 +199,10 @@ pub(crate) fn map_rust_diagnostic_to_lsp(
} }
} }
let mut needs_primary_span_label = true;
let mut related_information = vec![]; let mut related_information = vec![];
let mut tags = vec![]; let mut tags = vec![];
// If error occurs from macro expansion, add related info pointing to
// where the error originated
if !is_from_macro(&primary_span.file_name) && primary_span.expansion.is_some() {
let def_loc = map_span_to_location_naive(&primary_span, workspace_root);
related_information.push(DiagnosticRelatedInformation {
location: def_loc,
message: "Error originated from macro here".to_string(),
});
}
for secondary_span in rd.spans.iter().filter(|s| !s.is_primary) { for secondary_span in rd.spans.iter().filter(|s| !s.is_primary) {
let related = map_secondary_span_to_related(secondary_span, workspace_root); let related = map_secondary_span_to_related(secondary_span, workspace_root);
if let Some(related) = related { if let Some(related) = related {
@ -240,15 +222,11 @@ pub(crate) fn map_rust_diagnostic_to_lsp(
// These secondary messages usually duplicate the content of the // These secondary messages usually duplicate the content of the
// primary span label. // primary span label.
primary_span_label = None; needs_primary_span_label = false;
} }
} }
} }
if let Some(primary_span_label) = primary_span_label {
write!(&mut message, "\n{}", primary_span_label).unwrap();
}
if is_unused_or_unnecessary(rd) { if is_unused_or_unnecessary(rd) {
tags.push(DiagnosticTag::Unnecessary); tags.push(DiagnosticTag::Unnecessary);
} }
@ -257,21 +235,45 @@ pub(crate) fn map_rust_diagnostic_to_lsp(
tags.push(DiagnosticTag::Deprecated); tags.push(DiagnosticTag::Deprecated);
} }
let diagnostic = Diagnostic { primary_spans
range: location.range, .iter()
severity, .map(|primary_span| {
code: code.map(NumberOrString::String), let location = map_span_to_location(&primary_span, workspace_root);
source: Some(source),
message,
related_information: if !related_information.is_empty() {
Some(related_information)
} else {
None
},
tags: if !tags.is_empty() { Some(tags) } else { None },
};
Some(MappedRustDiagnostic { location, diagnostic, fixes }) let mut message = message.clone();
if needs_primary_span_label {
if let Some(primary_span_label) = &primary_span.label {
write!(&mut message, "\n{}", primary_span_label).unwrap();
}
}
// If error occurs from macro expansion, add related info pointing to
// where the error originated
if !is_from_macro(&primary_span.file_name) && primary_span.expansion.is_some() {
let def_loc = map_span_to_location_naive(&primary_span, workspace_root);
related_information.push(DiagnosticRelatedInformation {
location: def_loc,
message: "Error originated from macro here".to_string(),
});
}
let diagnostic = Diagnostic {
range: location.range,
severity,
code: code.clone().map(NumberOrString::String),
source: Some(source.clone()),
message,
related_information: if !related_information.is_empty() {
Some(related_information.clone())
} else {
None
},
tags: if !tags.is_empty() { Some(tags.clone()) } else { None },
};
MappedRustDiagnostic { location, diagnostic, fixes: fixes.clone() }
})
.collect()
} }
/// Returns a `Url` object from a given path, will lowercase drive letters if present. /// Returns a `Url` object from a given path, will lowercase drive letters if present.

View file

@ -2,98 +2,100 @@
source: crates/ra_cargo_watch/src/conv/test.rs source: crates/ra_cargo_watch/src/conv/test.rs
expression: diag expression: diag
--- ---
MappedRustDiagnostic { [
location: Location { MappedRustDiagnostic {
uri: "file:///test/compiler/mir/tagset.rs", location: Location {
range: Range { uri: "file:///test/compiler/mir/tagset.rs",
start: Position { range: Range {
line: 41, start: Position {
character: 23, line: 41,
}, character: 23,
end: Position {
line: 41,
character: 28,
},
},
},
diagnostic: Diagnostic {
range: Range {
start: Position {
line: 41,
character: 23,
},
end: Position {
line: 41,
character: 28,
},
},
severity: Some(
Warning,
),
code: Some(
String(
"trivially_copy_pass_by_ref",
),
),
source: Some(
"clippy",
),
message: "this argument is passed by reference, but would be more efficient if passed by value\n#[warn(clippy::trivially_copy_pass_by_ref)] implied by #[warn(clippy::all)]\nfor further information visit https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref",
related_information: Some(
[
DiagnosticRelatedInformation {
location: Location {
uri: "file:///test/compiler/lib.rs",
range: Range {
start: Position {
line: 0,
character: 8,
},
end: Position {
line: 0,
character: 19,
},
},
},
message: "lint level defined here",
}, },
], end: Position {
), line: 41,
tags: None, character: 28,
}, },
fixes: [ },
CodeAction { },
title: "consider passing by value instead: \'self\'", diagnostic: Diagnostic {
kind: Some( range: Range {
"quickfix", start: Position {
line: 41,
character: 23,
},
end: Position {
line: 41,
character: 28,
},
},
severity: Some(
Warning,
), ),
diagnostics: None, code: Some(
edit: Some( String(
WorkspaceEdit { "trivially_copy_pass_by_ref",
changes: Some( ),
{ ),
"file:///test/compiler/mir/tagset.rs": [ source: Some(
TextEdit { "clippy",
range: Range { ),
start: Position { message: "this argument is passed by reference, but would be more efficient if passed by value\n#[warn(clippy::trivially_copy_pass_by_ref)] implied by #[warn(clippy::all)]\nfor further information visit https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref",
line: 41, related_information: Some(
character: 23, [
}, DiagnosticRelatedInformation {
end: Position { location: Location {
line: 41, uri: "file:///test/compiler/lib.rs",
character: 28, range: Range {
}, start: Position {
}, line: 0,
new_text: "self", character: 8,
}, },
], end: Position {
line: 0,
character: 19,
},
},
}, },
), message: "lint level defined here",
document_changes: None, },
}, ],
), ),
command: None, tags: None,
is_preferred: None,
}, },
], fixes: [
} CodeAction {
title: "consider passing by value instead",
kind: Some(
"quickfix",
),
diagnostics: None,
edit: Some(
WorkspaceEdit {
changes: Some(
{
"file:///test/compiler/mir/tagset.rs": [
TextEdit {
range: Range {
start: Position {
line: 41,
character: 23,
},
end: Position {
line: 41,
character: 28,
},
},
new_text: "self",
},
],
},
),
document_changes: None,
},
),
command: None,
is_preferred: None,
},
],
},
]

View file

@ -2,45 +2,47 @@
source: crates/ra_cargo_watch/src/conv/test.rs source: crates/ra_cargo_watch/src/conv/test.rs
expression: diag expression: diag
--- ---
MappedRustDiagnostic { [
location: Location { MappedRustDiagnostic {
uri: "file:///test/src/main.rs", location: Location {
range: Range { uri: "file:///test/src/main.rs",
start: Position { range: Range {
line: 1, start: Position {
character: 4, line: 1,
}, character: 4,
end: Position { },
line: 1, end: Position {
character: 26, line: 1,
character: 26,
},
}, },
}, },
}, diagnostic: Diagnostic {
diagnostic: Diagnostic { range: Range {
range: Range { start: Position {
start: Position { line: 1,
line: 1, character: 4,
character: 4, },
end: Position {
line: 1,
character: 26,
},
}, },
end: Position { severity: Some(
line: 1, Error,
character: 26,
},
},
severity: Some(
Error,
),
code: Some(
String(
"E0277",
), ),
), code: Some(
source: Some( String(
"rustc", "E0277",
), ),
message: "can\'t compare `{integer}` with `&str`\nthe trait `std::cmp::PartialEq<&str>` is not implemented for `{integer}`", ),
related_information: None, source: Some(
tags: None, "rustc",
),
message: "can\'t compare `{integer}` with `&str`\nthe trait `std::cmp::PartialEq<&str>` is not implemented for `{integer}`",
related_information: None,
tags: None,
},
fixes: [],
}, },
fixes: [], ]
}

View file

@ -2,60 +2,62 @@
source: crates/ra_cargo_watch/src/conv/test.rs source: crates/ra_cargo_watch/src/conv/test.rs
expression: diag expression: diag
--- ---
MappedRustDiagnostic { [
location: Location { MappedRustDiagnostic {
uri: "file:///test/crates/ra_hir_def/src/data.rs", location: Location {
range: Range { uri: "file:///test/crates/ra_hir_def/src/data.rs",
start: Position { range: Range {
line: 79, start: Position {
character: 15, line: 79,
}, character: 15,
end: Position { },
line: 79, end: Position {
character: 41, line: 79,
character: 41,
},
}, },
}, },
}, diagnostic: Diagnostic {
diagnostic: Diagnostic { range: Range {
range: Range { start: Position {
start: Position { line: 79,
line: 79, character: 15,
character: 15, },
end: Position {
line: 79,
character: 41,
},
}, },
end: Position { severity: Some(
line: 79, Error,
character: 41, ),
}, code: None,
}, source: Some(
severity: Some( "rustc",
Error, ),
), message: "Please register your known path in the path module",
code: None, related_information: Some(
source: Some( [
"rustc", DiagnosticRelatedInformation {
), location: Location {
message: "Please register your known path in the path module", uri: "file:///test/crates/ra_hir_def/src/path.rs",
related_information: Some( range: Range {
[ start: Position {
DiagnosticRelatedInformation { line: 264,
location: Location { character: 8,
uri: "file:///test/crates/ra_hir_def/src/path.rs", },
range: Range { end: Position {
start: Position { line: 264,
line: 264, character: 76,
character: 8, },
},
end: Position {
line: 264,
character: 76,
}, },
}, },
message: "Error originated from macro here",
}, },
message: "Error originated from macro here", ],
}, ),
], tags: None,
), },
tags: None, fixes: [],
}, },
fixes: [], ]
}

View file

@ -0,0 +1,114 @@
---
source: crates/ra_cargo_watch/src/conv/test.rs
expression: diag
---
[
MappedRustDiagnostic {
location: Location {
uri: "file:///test/src/main.rs",
range: Range {
start: Position {
line: 3,
character: 4,
},
end: Position {
line: 3,
character: 5,
},
},
},
diagnostic: Diagnostic {
range: Range {
start: Position {
line: 3,
character: 4,
},
end: Position {
line: 3,
character: 5,
},
},
severity: Some(
Warning,
),
code: Some(
String(
"let_and_return",
),
),
source: Some(
"clippy",
),
message: "returning the result of a let binding from a block\n`#[warn(clippy::let_and_return)]` on by default\nfor further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_and_return",
related_information: Some(
[
DiagnosticRelatedInformation {
location: Location {
uri: "file:///test/src/main.rs",
range: Range {
start: Position {
line: 2,
character: 4,
},
end: Position {
line: 2,
character: 30,
},
},
},
message: "unnecessary let binding",
},
],
),
tags: None,
},
fixes: [
CodeAction {
title: "return the expression directly",
kind: Some(
"quickfix",
),
diagnostics: None,
edit: Some(
WorkspaceEdit {
changes: Some(
{
"file:///test/src/main.rs": [
TextEdit {
range: Range {
start: Position {
line: 2,
character: 4,
},
end: Position {
line: 2,
character: 30,
},
},
new_text: "",
},
TextEdit {
range: Range {
start: Position {
line: 3,
character: 4,
},
end: Position {
line: 3,
character: 5,
},
},
new_text: "(0..10).collect()",
},
],
},
),
document_changes: None,
},
),
command: None,
is_preferred: None,
},
],
},
]

View file

@ -2,45 +2,47 @@
source: crates/ra_cargo_watch/src/conv/test.rs source: crates/ra_cargo_watch/src/conv/test.rs
expression: diag expression: diag
--- ---
MappedRustDiagnostic { [
location: Location { MappedRustDiagnostic {
uri: "file:///test/compiler/ty/list_iter.rs", location: Location {
range: Range { uri: "file:///test/compiler/ty/list_iter.rs",
start: Position { range: Range {
line: 51, start: Position {
character: 4, line: 51,
}, character: 4,
end: Position { },
line: 51, end: Position {
character: 47, line: 51,
character: 47,
},
}, },
}, },
}, diagnostic: Diagnostic {
diagnostic: Diagnostic { range: Range {
range: Range { start: Position {
start: Position { line: 51,
line: 51, character: 4,
character: 4, },
end: Position {
line: 51,
character: 47,
},
}, },
end: Position { severity: Some(
line: 51, Error,
character: 47,
},
},
severity: Some(
Error,
),
code: Some(
String(
"E0053",
), ),
), code: Some(
source: Some( String(
"rustc", "E0053",
), ),
message: "method `next` has an incompatible type for trait\nexpected type `fn(&mut ty::list_iter::ListIterator<\'list, M>) -> std::option::Option<&ty::Ref<M>>`\n found type `fn(&ty::list_iter::ListIterator<\'list, M>) -> std::option::Option<&\'list ty::Ref<M>>`", ),
related_information: None, source: Some(
tags: None, "rustc",
),
message: "method `next` has an incompatible type for trait\nexpected type `fn(&mut ty::list_iter::ListIterator<\'list, M>) -> std::option::Option<&ty::Ref<M>>`\n found type `fn(&ty::list_iter::ListIterator<\'list, M>) -> std::option::Option<&\'list ty::Ref<M>>`",
related_information: None,
tags: None,
},
fixes: [],
}, },
fixes: [], ]
}

View file

@ -2,45 +2,47 @@
source: crates/ra_cargo_watch/src/conv/test.rs source: crates/ra_cargo_watch/src/conv/test.rs
expression: diag expression: diag
--- ---
MappedRustDiagnostic { [
location: Location { MappedRustDiagnostic {
uri: "file:///test/runtime/compiler_support.rs", location: Location {
range: Range { uri: "file:///test/runtime/compiler_support.rs",
start: Position { range: Range {
line: 47, start: Position {
character: 64, line: 47,
}, character: 64,
end: Position { },
line: 47, end: Position {
character: 69, line: 47,
character: 69,
},
}, },
}, },
}, diagnostic: Diagnostic {
diagnostic: Diagnostic { range: Range {
range: Range { start: Position {
start: Position { line: 47,
line: 47, character: 64,
character: 64, },
end: Position {
line: 47,
character: 69,
},
}, },
end: Position { severity: Some(
line: 47, Error,
character: 69,
},
},
severity: Some(
Error,
),
code: Some(
String(
"E0308",
), ),
), code: Some(
source: Some( String(
"rustc", "E0308",
), ),
message: "mismatched types\nexpected usize, found u32", ),
related_information: None, source: Some(
tags: None, "rustc",
),
message: "mismatched types\nexpected usize, found u32",
related_information: None,
tags: None,
},
fixes: [],
}, },
fixes: [], ]
}

View file

@ -2,83 +2,85 @@
source: crates/ra_cargo_watch/src/conv/test.rs source: crates/ra_cargo_watch/src/conv/test.rs
expression: diag expression: diag
--- ---
MappedRustDiagnostic { [
location: Location { MappedRustDiagnostic {
uri: "file:///test/driver/subcommand/repl.rs", location: Location {
range: Range { uri: "file:///test/driver/subcommand/repl.rs",
start: Position { range: Range {
line: 290, start: Position {
character: 8, line: 290,
}, character: 8,
end: Position {
line: 290,
character: 11,
},
},
},
diagnostic: Diagnostic {
range: Range {
start: Position {
line: 290,
character: 8,
},
end: Position {
line: 290,
character: 11,
},
},
severity: Some(
Warning,
),
code: Some(
String(
"unused_variables",
),
),
source: Some(
"rustc",
),
message: "unused variable: `foo`\n#[warn(unused_variables)] on by default",
related_information: None,
tags: Some(
[
Unnecessary,
],
),
},
fixes: [
CodeAction {
title: "consider prefixing with an underscore: \'_foo\'",
kind: Some(
"quickfix",
),
diagnostics: None,
edit: Some(
WorkspaceEdit {
changes: Some(
{
"file:///test/driver/subcommand/repl.rs": [
TextEdit {
range: Range {
start: Position {
line: 290,
character: 8,
},
end: Position {
line: 290,
character: 11,
},
},
new_text: "_foo",
},
],
},
),
document_changes: None,
}, },
), end: Position {
command: None, line: 290,
is_preferred: None, character: 11,
},
},
}, },
], diagnostic: Diagnostic {
} range: Range {
start: Position {
line: 290,
character: 8,
},
end: Position {
line: 290,
character: 11,
},
},
severity: Some(
Warning,
),
code: Some(
String(
"unused_variables",
),
),
source: Some(
"rustc",
),
message: "unused variable: `foo`\n#[warn(unused_variables)] on by default",
related_information: None,
tags: Some(
[
Unnecessary,
],
),
},
fixes: [
CodeAction {
title: "consider prefixing with an underscore",
kind: Some(
"quickfix",
),
diagnostics: None,
edit: Some(
WorkspaceEdit {
changes: Some(
{
"file:///test/driver/subcommand/repl.rs": [
TextEdit {
range: Range {
start: Position {
line: 290,
character: 8,
},
end: Position {
line: 290,
character: 11,
},
},
new_text: "_foo",
},
],
},
),
document_changes: None,
},
),
command: None,
is_preferred: None,
},
],
},
]

View file

@ -2,64 +2,66 @@
source: crates/ra_cargo_watch/src/conv/test.rs source: crates/ra_cargo_watch/src/conv/test.rs
expression: diag expression: diag
--- ---
MappedRustDiagnostic { [
location: Location { MappedRustDiagnostic {
uri: "file:///test/compiler/ty/select.rs", location: Location {
range: Range { uri: "file:///test/compiler/ty/select.rs",
start: Position { range: Range {
line: 103, start: Position {
character: 17, line: 103,
}, character: 17,
end: Position { },
line: 103, end: Position {
character: 29, line: 103,
character: 29,
},
}, },
}, },
}, diagnostic: Diagnostic {
diagnostic: Diagnostic { range: Range {
range: Range { start: Position {
start: Position { line: 103,
line: 103, character: 17,
character: 17, },
end: Position {
line: 103,
character: 29,
},
}, },
end: Position { severity: Some(
line: 103, Error,
character: 29,
},
},
severity: Some(
Error,
),
code: Some(
String(
"E0061",
), ),
), code: Some(
source: Some( String(
"rustc", "E0061",
), ),
message: "this function takes 2 parameters but 3 parameters were supplied\nexpected 2 parameters", ),
related_information: Some( source: Some(
[ "rustc",
DiagnosticRelatedInformation { ),
location: Location { message: "this function takes 2 parameters but 3 parameters were supplied\nexpected 2 parameters",
uri: "file:///test/compiler/ty/select.rs", related_information: Some(
range: Range { [
start: Position { DiagnosticRelatedInformation {
line: 218, location: Location {
character: 4, uri: "file:///test/compiler/ty/select.rs",
}, range: Range {
end: Position { start: Position {
line: 230, line: 218,
character: 5, character: 4,
},
end: Position {
line: 230,
character: 5,
},
}, },
}, },
message: "defined here",
}, },
message: "defined here", ],
}, ),
], tags: None,
), },
tags: None, fixes: [],
}, },
fixes: [], ]
}

View file

@ -58,7 +58,7 @@ fn snap_rustc_incompatible_type_for_trait() {
); );
let workspace_root = PathBuf::from("/test/"); let workspace_root = PathBuf::from("/test/");
let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root).expect("couldn't map diagnostic"); let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root);
insta::assert_debug_snapshot!(diag); insta::assert_debug_snapshot!(diag);
} }
@ -141,7 +141,7 @@ fn snap_rustc_unused_variable() {
); );
let workspace_root = PathBuf::from("/test/"); let workspace_root = PathBuf::from("/test/");
let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root).expect("couldn't map diagnostic"); let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root);
insta::assert_debug_snapshot!(diag); insta::assert_debug_snapshot!(diag);
} }
@ -266,7 +266,7 @@ fn snap_rustc_wrong_number_of_parameters() {
); );
let workspace_root = PathBuf::from("/test/"); let workspace_root = PathBuf::from("/test/");
let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root).expect("couldn't map diagnostic"); let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root);
insta::assert_debug_snapshot!(diag); insta::assert_debug_snapshot!(diag);
} }
@ -387,7 +387,7 @@ fn snap_clippy_pass_by_ref() {
); );
let workspace_root = PathBuf::from("/test/"); let workspace_root = PathBuf::from("/test/");
let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root).expect("couldn't map diagnostic"); let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root);
insta::assert_debug_snapshot!(diag); insta::assert_debug_snapshot!(diag);
} }
@ -431,7 +431,7 @@ fn snap_rustc_mismatched_type() {
); );
let workspace_root = PathBuf::from("/test/"); let workspace_root = PathBuf::from("/test/");
let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root).expect("couldn't map diagnostic"); let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root);
insta::assert_debug_snapshot!(diag); insta::assert_debug_snapshot!(diag);
} }
@ -703,7 +703,7 @@ fn snap_handles_macro_location() {
); );
let workspace_root = PathBuf::from("/test/"); let workspace_root = PathBuf::from("/test/");
let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root).expect("couldn't map diagnostic"); let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root);
insta::assert_debug_snapshot!(diag); insta::assert_debug_snapshot!(diag);
} }
@ -933,6 +933,140 @@ fn snap_macro_compiler_error() {
); );
let workspace_root = PathBuf::from("/test/"); let workspace_root = PathBuf::from("/test/");
let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root).expect("couldn't map diagnostic"); let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root);
insta::assert_debug_snapshot!(diag);
}
#[test]
#[cfg(not(windows))]
fn snap_multi_line_fix() {
let diag = parse_diagnostic(
r##"{
"rendered": "warning: returning the result of a let binding from a block\n --> src/main.rs:4:5\n |\n3 | let a = (0..10).collect();\n | -------------------------- unnecessary let binding\n4 | a\n | ^\n |\n = note: `#[warn(clippy::let_and_return)]` on by default\n = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_and_return\nhelp: return the expression directly\n |\n3 | \n4 | (0..10).collect()\n |\n\n",
"children": [
{
"children": [],
"code": null,
"level": "note",
"message": "`#[warn(clippy::let_and_return)]` on by default",
"rendered": null,
"spans": []
},
{
"children": [],
"code": null,
"level": "help",
"message": "for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_and_return",
"rendered": null,
"spans": []
},
{
"children": [],
"code": null,
"level": "help",
"message": "return the expression directly",
"rendered": null,
"spans": [
{
"byte_end": 55,
"byte_start": 29,
"column_end": 31,
"column_start": 5,
"expansion": null,
"file_name": "src/main.rs",
"is_primary": true,
"label": null,
"line_end": 3,
"line_start": 3,
"suggested_replacement": "",
"suggestion_applicability": "MachineApplicable",
"text": [
{
"highlight_end": 31,
"highlight_start": 5,
"text": " let a = (0..10).collect();"
}
]
},
{
"byte_end": 61,
"byte_start": 60,
"column_end": 6,
"column_start": 5,
"expansion": null,
"file_name": "src/main.rs",
"is_primary": true,
"label": null,
"line_end": 4,
"line_start": 4,
"suggested_replacement": "(0..10).collect()",
"suggestion_applicability": "MachineApplicable",
"text": [
{
"highlight_end": 6,
"highlight_start": 5,
"text": " a"
}
]
}
]
}
],
"code": {
"code": "clippy::let_and_return",
"explanation": null
},
"level": "warning",
"message": "returning the result of a let binding from a block",
"spans": [
{
"byte_end": 55,
"byte_start": 29,
"column_end": 31,
"column_start": 5,
"expansion": null,
"file_name": "src/main.rs",
"is_primary": false,
"label": "unnecessary let binding",
"line_end": 3,
"line_start": 3,
"suggested_replacement": null,
"suggestion_applicability": null,
"text": [
{
"highlight_end": 31,
"highlight_start": 5,
"text": " let a = (0..10).collect();"
}
]
},
{
"byte_end": 61,
"byte_start": 60,
"column_end": 6,
"column_start": 5,
"expansion": null,
"file_name": "src/main.rs",
"is_primary": true,
"label": null,
"line_end": 4,
"line_start": 4,
"suggested_replacement": null,
"suggestion_applicability": null,
"text": [
{
"highlight_end": 6,
"highlight_start": 5,
"text": " a"
}
]
}
]
}
"##,
);
let workspace_root = PathBuf::from("/test/");
let diag = map_rust_diagnostic_to_lsp(&diag, &workspace_root);
insta::assert_debug_snapshot!(diag); insta::assert_debug_snapshot!(diag);
} }

View file

@ -197,23 +197,23 @@ impl CheckWatcherThread {
} }
CheckEvent::Msg(Message::CompilerMessage(msg)) => { CheckEvent::Msg(Message::CompilerMessage(msg)) => {
let map_result = let map_result = map_rust_diagnostic_to_lsp(&msg.message, &self.workspace_root);
match map_rust_diagnostic_to_lsp(&msg.message, &self.workspace_root) { if map_result.is_empty() {
Some(map_result) => map_result, return;
None => return, }
};
let MappedRustDiagnostic { location, diagnostic, fixes } = map_result; for MappedRustDiagnostic { location, diagnostic, fixes } in map_result {
let fixes = fixes let fixes = fixes
.into_iter() .into_iter()
.map(|fix| { .map(|fix| {
CodeAction { diagnostics: Some(vec![diagnostic.clone()]), ..fix }.into() CodeAction { diagnostics: Some(vec![diagnostic.clone()]), ..fix }.into()
}) })
.collect(); .collect();
task_send task_send
.send(CheckTask::AddDiagnostic { url: location.uri, diagnostic, fixes }) .send(CheckTask::AddDiagnostic { url: location.uri, diagnostic, fixes })
.unwrap(); .unwrap();
}
} }
CheckEvent::Msg(Message::BuildScriptExecuted(_msg)) => {} CheckEvent::Msg(Message::BuildScriptExecuted(_msg)) => {}