Skip to content

Commit a8a1bc4

Browse files
committed
Extract lint scopes from cargo watch
Currently all of our VS Code diagnostics are given the source of `rustc`. However, if you have something like `cargo-watch.command` set to `clippy` it will also watch for Clippy lints. The `rustc` source is a bit misleading in that case. Fortunately, Rust's tool lints (RFC 2103) line up perfectly with VS Code's concept of `source`. This checks for lints scoped to a given tool and then splits them in to a `source` and tool-specific `code`.
1 parent 04a211f commit a8a1bc4

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

editors/code/src/test/rust_diagnostics.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ describe('mapRustDiagnosticToVsCode', () => {
3737
diagnostic.severity,
3838
vscode.DiagnosticSeverity.Error
3939
);
40+
assert.strictEqual(diagnostic.source, 'rustc');
4041
assert.strictEqual(
4142
diagnostic.message,
4243
[
@@ -72,6 +73,7 @@ describe('mapRustDiagnosticToVsCode', () => {
7273
].join('\n')
7374
);
7475
assert.strictEqual(diagnostic.code, 'unused_variables');
76+
assert.strictEqual(diagnostic.source, 'rustc');
7577
assert.deepStrictEqual(diagnostic.tags, [
7678
vscode.DiagnosticTag.Unnecessary
7779
]);
@@ -101,6 +103,7 @@ describe('mapRustDiagnosticToVsCode', () => {
101103
'this function takes 2 parameters but 3 parameters were supplied'
102104
);
103105
assert.strictEqual(diagnostic.code, 'E0061');
106+
assert.strictEqual(diagnostic.source, 'rustc');
104107
assert.strictEqual(diagnostic.tags, undefined);
105108

106109
// One related information for the original definition
@@ -125,6 +128,7 @@ describe('mapRustDiagnosticToVsCode', () => {
125128
diagnostic.severity,
126129
vscode.DiagnosticSeverity.Warning
127130
);
131+
assert.strictEqual(diagnostic.source, 'clippy');
128132
assert.strictEqual(
129133
diagnostic.message,
130134
[
@@ -133,10 +137,7 @@ describe('mapRustDiagnosticToVsCode', () => {
133137
'for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref'
134138
].join('\n')
135139
);
136-
assert.strictEqual(
137-
diagnostic.code,
138-
'clippy::trivially_copy_pass_by_ref'
139-
);
140+
assert.strictEqual(diagnostic.code, 'trivially_copy_pass_by_ref');
140141
assert.strictEqual(diagnostic.tags, undefined);
141142

142143
// One related information for the lint definition

editors/code/src/test/vscode_diagnostics.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,24 @@ describe('areDiagnosticsEqual', () => {
3535
assert(areDiagnosticsEqual(diagnostic1, diagnostic2));
3636
});
3737

38+
it('should treat diagnostics with different sources as inequal', () => {
39+
const diagnostic1 = new vscode.Diagnostic(
40+
range1,
41+
'Hello, world!',
42+
vscode.DiagnosticSeverity.Error
43+
);
44+
diagnostic1.source = 'rustc';
45+
46+
const diagnostic2 = new vscode.Diagnostic(
47+
range1,
48+
'Hello, world!',
49+
vscode.DiagnosticSeverity.Error
50+
);
51+
diagnostic2.source = 'clippy';
52+
53+
assert(!areDiagnosticsEqual(diagnostic1, diagnostic2));
54+
});
55+
3856
it('should treat diagnostics with different ranges as inequal', () => {
3957
const diagnostic1 = new vscode.Diagnostic(
4058
range1,

editors/code/src/utils/rust_diagnostics.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,18 @@ export function mapRustDiagnosticToVsCode(
187187

188188
const vd = new vscode.Diagnostic(location.range, rd.message, severity);
189189

190-
vd.source = 'rustc';
191-
vd.code = rd.code ? rd.code.code : undefined;
190+
let source = 'rustc';
191+
let code = rd.code && rd.code.code;
192+
if (code) {
193+
// See if this is an RFC #2103 scoped lint (e.g. from Clippy)
194+
const scopedCode = code.split('::');
195+
if (scopedCode.length === 2) {
196+
[source, code] = scopedCode;
197+
}
198+
}
199+
200+
vd.source = source;
201+
vd.code = code;
192202
vd.relatedInformation = [];
193203

194204
for (const secondarySpan of secondarySpans) {

0 commit comments

Comments
 (0)