From efe662d474a36937702e87e7eed980c7aed6276d Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Fri, 6 Aug 2021 15:52:47 +0200 Subject: [PATCH 1/3] Include suggested replacement in diagnostics --- .../rust-analyzer/src/diagnostics/to_proto.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/crates/rust-analyzer/src/diagnostics/to_proto.rs b/crates/rust-analyzer/src/diagnostics/to_proto.rs index d64909add38..e60b939015a 100644 --- a/crates/rust-analyzer/src/diagnostics/to_proto.rs +++ b/crates/rust-analyzer/src/diagnostics/to_proto.rs @@ -3,6 +3,7 @@ use std::collections::HashMap; use flycheck::{DiagnosticLevel, DiagnosticSpan}; +use itertools::Itertools; use stdx::format_to; use vfs::{AbsPath, AbsPathBuf}; @@ -134,19 +135,31 @@ fn map_rust_child_diagnostic( } let mut edit_map: HashMap> = HashMap::new(); + let mut suggested_replacements = Vec::new(); for &span in &spans { if let Some(suggested_replacement) = &span.suggested_replacement { + suggested_replacements.push(suggested_replacement); let location = location(config, workspace_root, span); let edit = lsp_types::TextEdit::new(location.range, suggested_replacement.clone()); edit_map.entry(location.uri).or_default().push(edit); } } + // rustc renders suggestion diagnostics by appending the suggested replacement, so do the same + // here, otherwise the diagnostic text is missing useful information. + let mut message = rd.message.clone(); + if !suggested_replacements.is_empty() { + message.push_str(": "); + let suggestions = + suggested_replacements.iter().map(|suggestion| format!("`{}`", suggestion)).join(", "); + message.push_str(&suggestions); + } + if edit_map.is_empty() { MappedRustChildDiagnostic::SubDiagnostic(SubDiagnostic { related: lsp_types::DiagnosticRelatedInformation { location: location(config, workspace_root, spans[0]), - message: rd.message.clone(), + message, }, suggested_fix: None, }) @@ -154,10 +167,10 @@ fn map_rust_child_diagnostic( MappedRustChildDiagnostic::SubDiagnostic(SubDiagnostic { related: lsp_types::DiagnosticRelatedInformation { location: location(config, workspace_root, spans[0]), - message: rd.message.clone(), + message: message.clone(), }, suggested_fix: Some(lsp_ext::CodeAction { - title: rd.message.clone(), + title: message, group: None, kind: Some(lsp_types::CodeActionKind::QUICKFIX), edit: Some(lsp_ext::SnippetWorkspaceEdit { From 30e472a12ae2d0d2cb843e93fb58ba4bcc7b4ab4 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Fri, 6 Aug 2021 15:58:55 +0200 Subject: [PATCH 2/3] Don't include empty suggestions --- crates/rust-analyzer/src/diagnostics/to_proto.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/rust-analyzer/src/diagnostics/to_proto.rs b/crates/rust-analyzer/src/diagnostics/to_proto.rs index e60b939015a..4739cabae55 100644 --- a/crates/rust-analyzer/src/diagnostics/to_proto.rs +++ b/crates/rust-analyzer/src/diagnostics/to_proto.rs @@ -138,7 +138,9 @@ fn map_rust_child_diagnostic( let mut suggested_replacements = Vec::new(); for &span in &spans { if let Some(suggested_replacement) = &span.suggested_replacement { - suggested_replacements.push(suggested_replacement); + if !suggested_replacement.is_empty() { + suggested_replacements.push(suggested_replacement); + } let location = location(config, workspace_root, span); let edit = lsp_types::TextEdit::new(location.range, suggested_replacement.clone()); edit_map.entry(location.uri).or_default().push(edit); From 5386dc648edb96b42da52974beac5b395f411210 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Fri, 6 Aug 2021 15:59:58 +0200 Subject: [PATCH 3/3] Bless tests --- .../src/diagnostics/test_data/clippy_pass_by_ref.txt | 6 +++--- .../src/diagnostics/test_data/rustc_unused_variable.txt | 6 +++--- .../diagnostics/test_data/rustc_unused_variable_as_hint.txt | 6 +++--- .../diagnostics/test_data/rustc_unused_variable_as_info.txt | 6 +++--- .../src/diagnostics/test_data/snap_multi_line_fix.txt | 6 +++--- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/crates/rust-analyzer/src/diagnostics/test_data/clippy_pass_by_ref.txt b/crates/rust-analyzer/src/diagnostics/test_data/clippy_pass_by_ref.txt index bcc889681e8..fd04163de98 100644 --- a/crates/rust-analyzer/src/diagnostics/test_data/clippy_pass_by_ref.txt +++ b/crates/rust-analyzer/src/diagnostics/test_data/clippy_pass_by_ref.txt @@ -107,7 +107,7 @@ }, }, }, - message: "consider passing by value instead", + message: "consider passing by value instead: `self`", }, ], ), @@ -262,7 +262,7 @@ source: Some( "clippy", ), - message: "consider passing by value instead", + message: "consider passing by value instead: `self`", related_information: Some( [ DiagnosticRelatedInformation { @@ -298,7 +298,7 @@ }, fixes: [ CodeAction { - title: "consider passing by value instead", + title: "consider passing by value instead: `self`", group: None, kind: Some( CodeActionKind( diff --git a/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable.txt b/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable.txt index 749f49438fb..3ded70411d3 100644 --- a/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable.txt +++ b/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable.txt @@ -61,7 +61,7 @@ }, }, }, - message: "consider prefixing with an underscore", + message: "consider prefixing with an underscore: `_foo`", }, ], ), @@ -109,7 +109,7 @@ source: Some( "rustc", ), - message: "consider prefixing with an underscore", + message: "consider prefixing with an underscore: `_foo`", related_information: Some( [ DiagnosticRelatedInformation { @@ -145,7 +145,7 @@ }, fixes: [ CodeAction { - title: "consider prefixing with an underscore", + title: "consider prefixing with an underscore: `_foo`", group: None, kind: Some( CodeActionKind( diff --git a/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_hint.txt b/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_hint.txt index d20a91b3972..0993aa1af14 100644 --- a/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_hint.txt +++ b/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_hint.txt @@ -61,7 +61,7 @@ }, }, }, - message: "consider prefixing with an underscore", + message: "consider prefixing with an underscore: `_foo`", }, ], ), @@ -109,7 +109,7 @@ source: Some( "rustc", ), - message: "consider prefixing with an underscore", + message: "consider prefixing with an underscore: `_foo`", related_information: Some( [ DiagnosticRelatedInformation { @@ -145,7 +145,7 @@ }, fixes: [ CodeAction { - title: "consider prefixing with an underscore", + title: "consider prefixing with an underscore: `_foo`", group: None, kind: Some( CodeActionKind( diff --git a/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_info.txt b/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_info.txt index 2a9c3a9afe4..d7a974ae579 100644 --- a/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_info.txt +++ b/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_info.txt @@ -61,7 +61,7 @@ }, }, }, - message: "consider prefixing with an underscore", + message: "consider prefixing with an underscore: `_foo`", }, ], ), @@ -109,7 +109,7 @@ source: Some( "rustc", ), - message: "consider prefixing with an underscore", + message: "consider prefixing with an underscore: `_foo`", related_information: Some( [ DiagnosticRelatedInformation { @@ -145,7 +145,7 @@ }, fixes: [ CodeAction { - title: "consider prefixing with an underscore", + title: "consider prefixing with an underscore: `_foo`", group: None, kind: Some( CodeActionKind( diff --git a/crates/rust-analyzer/src/diagnostics/test_data/snap_multi_line_fix.txt b/crates/rust-analyzer/src/diagnostics/test_data/snap_multi_line_fix.txt index 43b1ea765ec..f77abde5155 100644 --- a/crates/rust-analyzer/src/diagnostics/test_data/snap_multi_line_fix.txt +++ b/crates/rust-analyzer/src/diagnostics/test_data/snap_multi_line_fix.txt @@ -107,7 +107,7 @@ }, }, }, - message: "return the expression directly", + message: "return the expression directly: `(0..10).collect()`", }, ], ), @@ -262,7 +262,7 @@ source: Some( "clippy", ), - message: "return the expression directly", + message: "return the expression directly: `(0..10).collect()`", related_information: Some( [ DiagnosticRelatedInformation { @@ -298,7 +298,7 @@ }, fixes: [ CodeAction { - title: "return the expression directly", + title: "return the expression directly: `(0..10).collect()`", group: None, kind: Some( CodeActionKind(