1 import * as assert from 'assert';
2 import * as fs from 'fs';
3 import * as vscode from 'vscode';
7 mapRustDiagnosticToVsCode,
9 SuggestionApplicability,
10 } from '../../../utils/diagnostics/rust';
12 function loadDiagnosticFixture(name: string): RustDiagnostic {
15 // We're actually in our JavaScript output directory, climb out
16 `${__dirname}/../../../../src/test/fixtures/rust-diagnostics/${name}.json`,
20 return JSON.parse(jsonText);
23 function mapFixtureToVsCode(name: string): MappedRustDiagnostic {
24 const rd = loadDiagnosticFixture(name);
25 const mapResult = mapRustDiagnosticToVsCode(rd);
28 return assert.fail('Mapping unexpectedly failed');
33 describe('mapRustDiagnosticToVsCode', () => {
34 it('should map an incompatible type for trait error', () => {
35 const { diagnostic, suggestedFixes } = mapFixtureToVsCode(
41 vscode.DiagnosticSeverity.Error,
43 assert.strictEqual(diagnostic.source, 'rustc');
47 `method \`next\` has an incompatible type for trait`,
48 `expected type \`fn(&mut ty::list_iter::ListIterator<'list, M>) -> std::option::Option<&ty::Ref<M>>\``,
49 ` found type \`fn(&ty::list_iter::ListIterator<'list, M>) -> std::option::Option<&'list ty::Ref<M>>\``,
52 assert.strictEqual(diagnostic.code, 'E0053');
53 assert.deepStrictEqual(diagnostic.tags, []);
55 // No related information
56 assert.deepStrictEqual(diagnostic.relatedInformation, []);
58 // There are no suggested fixes
59 assert.strictEqual(suggestedFixes.length, 0);
62 it('should map an unused variable warning', () => {
63 const { diagnostic, suggestedFixes } = mapFixtureToVsCode(
64 'warning/unused_variables',
69 vscode.DiagnosticSeverity.Warning,
74 'unused variable: `foo`',
75 '#[warn(unused_variables)] on by default',
78 assert.strictEqual(diagnostic.code, 'unused_variables');
79 assert.strictEqual(diagnostic.source, 'rustc');
80 assert.deepStrictEqual(diagnostic.tags, [
81 vscode.DiagnosticTag.Unnecessary,
84 // No related information
85 assert.deepStrictEqual(diagnostic.relatedInformation, []);
87 // One suggested fix available to prefix the variable
88 assert.strictEqual(suggestedFixes.length, 1);
89 const [suggestedFix] = suggestedFixes;
92 'consider prefixing with an underscore: `_foo`',
95 suggestedFix.applicability,
96 SuggestionApplicability.MachineApplicable,
100 it('should map a wrong number of parameters error', () => {
101 const { diagnostic, suggestedFixes } = mapFixtureToVsCode(
107 vscode.DiagnosticSeverity.Error,
112 'this function takes 2 parameters but 3 parameters were supplied',
113 'expected 2 parameters',
116 assert.strictEqual(diagnostic.code, 'E0061');
117 assert.strictEqual(diagnostic.source, 'rustc');
118 assert.deepStrictEqual(diagnostic.tags, []);
120 // One related information for the original definition
121 const relatedInformation = diagnostic.relatedInformation;
122 if (!relatedInformation) {
123 return assert.fail('Related information unexpectedly undefined');
125 assert.strictEqual(relatedInformation.length, 1);
126 const [related] = relatedInformation;
127 assert.strictEqual(related.message, 'defined here');
129 // There are no suggested fixes
130 assert.strictEqual(suggestedFixes.length, 0);
133 it('should map a Clippy copy pass by ref warning', () => {
134 const { diagnostic, suggestedFixes } = mapFixtureToVsCode(
135 'clippy/trivially_copy_pass_by_ref',
140 vscode.DiagnosticSeverity.Warning,
142 assert.strictEqual(diagnostic.source, 'clippy');
146 'this argument is passed by reference, but would be more efficient if passed by value',
147 '#[warn(clippy::trivially_copy_pass_by_ref)] implied by #[warn(clippy::all)]',
148 'for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref',
151 assert.strictEqual(diagnostic.code, 'trivially_copy_pass_by_ref');
152 assert.deepStrictEqual(diagnostic.tags, []);
154 // One related information for the lint definition
155 const relatedInformation = diagnostic.relatedInformation;
156 if (!relatedInformation) {
157 return assert.fail('Related information unexpectedly undefined');
159 assert.strictEqual(relatedInformation.length, 1);
160 const [related] = relatedInformation;
161 assert.strictEqual(related.message, 'lint level defined here');
163 // One suggested fix to pass by value
164 assert.strictEqual(suggestedFixes.length, 1);
165 const [suggestedFix] = suggestedFixes;
168 'consider passing by value instead: `self`',
170 // Clippy does not mark this with any applicability
172 suggestedFix.applicability,
173 SuggestionApplicability.Unspecified,
177 it('should map a mismatched type error', () => {
178 const { diagnostic, suggestedFixes } = mapFixtureToVsCode(
184 vscode.DiagnosticSeverity.Error,
188 ['mismatched types', 'expected usize, found u32'].join('\n'),
190 assert.strictEqual(diagnostic.code, 'E0308');
191 assert.strictEqual(diagnostic.source, 'rustc');
192 assert.deepStrictEqual(diagnostic.tags, []);
194 // No related information
195 assert.deepStrictEqual(diagnostic.relatedInformation, []);
197 // There are no suggested fixes
198 assert.strictEqual(suggestedFixes.length, 0);