]> git.lizzy.rs Git - rust.git/commitdiff
Extract lint scopes from `cargo watch`
authorRyan Cumming <etaoins@gmail.com>
Wed, 26 Jun 2019 22:47:36 +0000 (08:47 +1000)
committerRyan Cumming <etaoins@gmail.com>
Wed, 26 Jun 2019 22:52:22 +0000 (08:52 +1000)
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`.

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

index 5eb064b979a2097058041770b43e49b9f227d5f1..f27c58fe291b990d813071db878096d6e08c3e1c 100644 (file)
@@ -37,6 +37,7 @@ describe('mapRustDiagnosticToVsCode', () => {
             diagnostic.severity,
             vscode.DiagnosticSeverity.Error
         );
+        assert.strictEqual(diagnostic.source, 'rustc');
         assert.strictEqual(
             diagnostic.message,
             [
@@ -72,6 +73,7 @@ describe('mapRustDiagnosticToVsCode', () => {
             ].join('\n')
         );
         assert.strictEqual(diagnostic.code, 'unused_variables');
+        assert.strictEqual(diagnostic.source, 'rustc');
         assert.deepStrictEqual(diagnostic.tags, [
             vscode.DiagnosticTag.Unnecessary
         ]);
@@ -101,6 +103,7 @@ describe('mapRustDiagnosticToVsCode', () => {
             'this function takes 2 parameters but 3 parameters were supplied'
         );
         assert.strictEqual(diagnostic.code, 'E0061');
+        assert.strictEqual(diagnostic.source, 'rustc');
         assert.strictEqual(diagnostic.tags, undefined);
 
         // One related information for the original definition
@@ -125,6 +128,7 @@ describe('mapRustDiagnosticToVsCode', () => {
             diagnostic.severity,
             vscode.DiagnosticSeverity.Warning
         );
+        assert.strictEqual(diagnostic.source, 'clippy');
         assert.strictEqual(
             diagnostic.message,
             [
@@ -133,10 +137,7 @@ describe('mapRustDiagnosticToVsCode', () => {
                 'for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref'
             ].join('\n')
         );
-        assert.strictEqual(
-            diagnostic.code,
-            'clippy::trivially_copy_pass_by_ref'
-        );
+        assert.strictEqual(diagnostic.code, 'trivially_copy_pass_by_ref');
         assert.strictEqual(diagnostic.tags, undefined);
 
         // One related information for the lint definition
index ca43456269e46f38a98bd0fa1dfc63c43f72b880..9c5d812fa3bb99c08b4b9652b405efe7098135ee 100644 (file)
@@ -35,6 +35,24 @@ describe('areDiagnosticsEqual', () => {
         assert(areDiagnosticsEqual(diagnostic1, diagnostic2));
     });
 
+    it('should treat diagnostics with different sources as inequal', () => {
+        const diagnostic1 = new vscode.Diagnostic(
+            range1,
+            'Hello, world!',
+            vscode.DiagnosticSeverity.Error
+        );
+        diagnostic1.source = 'rustc';
+
+        const diagnostic2 = new vscode.Diagnostic(
+            range1,
+            'Hello, world!',
+            vscode.DiagnosticSeverity.Error
+        );
+        diagnostic2.source = 'clippy';
+
+        assert(!areDiagnosticsEqual(diagnostic1, diagnostic2));
+    });
+
     it('should treat diagnostics with different ranges as inequal', () => {
         const diagnostic1 = new vscode.Diagnostic(
             range1,
index ed049c95ef0db60f594565c1216daee4b115e9a8..3c524cb378093a95324622496ef92ad8ca8359e4 100644 (file)
@@ -187,8 +187,18 @@ export function mapRustDiagnosticToVsCode(
 
     const vd = new vscode.Diagnostic(location.range, rd.message, severity);
 
-    vd.source = 'rustc';
-    vd.code = rd.code ? rd.code.code : undefined;
+    let source = 'rustc';
+    let code = rd.code && rd.code.code;
+    if (code) {
+        // See if this is an RFC #2103 scoped lint (e.g. from Clippy)
+        const scopedCode = code.split('::');
+        if (scopedCode.length === 2) {
+            [source, code] = scopedCode;
+        }
+    }
+
+    vd.source = source;
+    vd.code = code;
     vd.relatedInformation = [];
 
     for (const secondarySpan of secondarySpans) {