Skip to content

Commit 40e9c97

Browse files
Merge #9800
9800: feat: Include suggested replacement in diagnostics r=jonas-schievink a=jonas-schievink rustc renders the diagnostic text for suggestions by including the suggested replacement at the end (`` help: a function with a similar name exists: `boo` ``), but the emitted JSON diagnostic does not include this in the message. This causes our diagnostics to lack some useful info, so this PR fixes that by appending any suggested replacements to the message. Fixes #9797 Before: ![screenshot-2021-08-06-15:54:19](https://user-images.githubusercontent.com/1786438/128521003-105a43a3-e386-4afc-9d5c-7806631f53d7.png) After: ![screenshot-2021-08-06-15:53:16](https://user-images.githubusercontent.com/1786438/128521022-c16e0967-6cc6-410d-917d-5db5cfbb96be.png) bors r+ Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
2 parents 85d80df + 5386dc6 commit 40e9c97

File tree

6 files changed

+33
-18
lines changed

6 files changed

+33
-18
lines changed

crates/rust-analyzer/src/diagnostics/test_data/clippy_pass_by_ref.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
},
108108
},
109109
},
110-
message: "consider passing by value instead",
110+
message: "consider passing by value instead: `self`",
111111
},
112112
],
113113
),
@@ -262,7 +262,7 @@
262262
source: Some(
263263
"clippy",
264264
),
265-
message: "consider passing by value instead",
265+
message: "consider passing by value instead: `self`",
266266
related_information: Some(
267267
[
268268
DiagnosticRelatedInformation {
@@ -298,7 +298,7 @@
298298
},
299299
fixes: [
300300
CodeAction {
301-
title: "consider passing by value instead",
301+
title: "consider passing by value instead: `self`",
302302
group: None,
303303
kind: Some(
304304
CodeActionKind(

crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
},
6262
},
6363
},
64-
message: "consider prefixing with an underscore",
64+
message: "consider prefixing with an underscore: `_foo`",
6565
},
6666
],
6767
),
@@ -109,7 +109,7 @@
109109
source: Some(
110110
"rustc",
111111
),
112-
message: "consider prefixing with an underscore",
112+
message: "consider prefixing with an underscore: `_foo`",
113113
related_information: Some(
114114
[
115115
DiagnosticRelatedInformation {
@@ -145,7 +145,7 @@
145145
},
146146
fixes: [
147147
CodeAction {
148-
title: "consider prefixing with an underscore",
148+
title: "consider prefixing with an underscore: `_foo`",
149149
group: None,
150150
kind: Some(
151151
CodeActionKind(

crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_hint.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
},
6262
},
6363
},
64-
message: "consider prefixing with an underscore",
64+
message: "consider prefixing with an underscore: `_foo`",
6565
},
6666
],
6767
),
@@ -109,7 +109,7 @@
109109
source: Some(
110110
"rustc",
111111
),
112-
message: "consider prefixing with an underscore",
112+
message: "consider prefixing with an underscore: `_foo`",
113113
related_information: Some(
114114
[
115115
DiagnosticRelatedInformation {
@@ -145,7 +145,7 @@
145145
},
146146
fixes: [
147147
CodeAction {
148-
title: "consider prefixing with an underscore",
148+
title: "consider prefixing with an underscore: `_foo`",
149149
group: None,
150150
kind: Some(
151151
CodeActionKind(

crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_info.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
},
6262
},
6363
},
64-
message: "consider prefixing with an underscore",
64+
message: "consider prefixing with an underscore: `_foo`",
6565
},
6666
],
6767
),
@@ -109,7 +109,7 @@
109109
source: Some(
110110
"rustc",
111111
),
112-
message: "consider prefixing with an underscore",
112+
message: "consider prefixing with an underscore: `_foo`",
113113
related_information: Some(
114114
[
115115
DiagnosticRelatedInformation {
@@ -145,7 +145,7 @@
145145
},
146146
fixes: [
147147
CodeAction {
148-
title: "consider prefixing with an underscore",
148+
title: "consider prefixing with an underscore: `_foo`",
149149
group: None,
150150
kind: Some(
151151
CodeActionKind(

crates/rust-analyzer/src/diagnostics/test_data/snap_multi_line_fix.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
},
108108
},
109109
},
110-
message: "return the expression directly",
110+
message: "return the expression directly: `(0..10).collect()`",
111111
},
112112
],
113113
),
@@ -262,7 +262,7 @@
262262
source: Some(
263263
"clippy",
264264
),
265-
message: "return the expression directly",
265+
message: "return the expression directly: `(0..10).collect()`",
266266
related_information: Some(
267267
[
268268
DiagnosticRelatedInformation {
@@ -298,7 +298,7 @@
298298
},
299299
fixes: [
300300
CodeAction {
301-
title: "return the expression directly",
301+
title: "return the expression directly: `(0..10).collect()`",
302302
group: None,
303303
kind: Some(
304304
CodeActionKind(

crates/rust-analyzer/src/diagnostics/to_proto.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use std::collections::HashMap;
44

55
use flycheck::{DiagnosticLevel, DiagnosticSpan};
6+
use itertools::Itertools;
67
use stdx::format_to;
78
use vfs::{AbsPath, AbsPathBuf};
89

@@ -134,30 +135,44 @@ fn map_rust_child_diagnostic(
134135
}
135136

136137
let mut edit_map: HashMap<lsp_types::Url, Vec<lsp_types::TextEdit>> = HashMap::new();
138+
let mut suggested_replacements = Vec::new();
137139
for &span in &spans {
138140
if let Some(suggested_replacement) = &span.suggested_replacement {
141+
if !suggested_replacement.is_empty() {
142+
suggested_replacements.push(suggested_replacement);
143+
}
139144
let location = location(config, workspace_root, span);
140145
let edit = lsp_types::TextEdit::new(location.range, suggested_replacement.clone());
141146
edit_map.entry(location.uri).or_default().push(edit);
142147
}
143148
}
144149

150+
// rustc renders suggestion diagnostics by appending the suggested replacement, so do the same
151+
// here, otherwise the diagnostic text is missing useful information.
152+
let mut message = rd.message.clone();
153+
if !suggested_replacements.is_empty() {
154+
message.push_str(": ");
155+
let suggestions =
156+
suggested_replacements.iter().map(|suggestion| format!("`{}`", suggestion)).join(", ");
157+
message.push_str(&suggestions);
158+
}
159+
145160
if edit_map.is_empty() {
146161
MappedRustChildDiagnostic::SubDiagnostic(SubDiagnostic {
147162
related: lsp_types::DiagnosticRelatedInformation {
148163
location: location(config, workspace_root, spans[0]),
149-
message: rd.message.clone(),
164+
message,
150165
},
151166
suggested_fix: None,
152167
})
153168
} else {
154169
MappedRustChildDiagnostic::SubDiagnostic(SubDiagnostic {
155170
related: lsp_types::DiagnosticRelatedInformation {
156171
location: location(config, workspace_root, spans[0]),
157-
message: rd.message.clone(),
172+
message: message.clone(),
158173
},
159174
suggested_fix: Some(lsp_ext::CodeAction {
160-
title: rd.message.clone(),
175+
title: message,
161176
group: None,
162177
kind: Some(lsp_types::CodeActionKind::QUICKFIX),
163178
edit: Some(lsp_ext::SnippetWorkspaceEdit {

0 commit comments

Comments
 (0)