Skip to content

Commit 26f2206

Browse files
pqCommit Queue
authored andcommitted
[fix] fix unnecessary_ignore eol ignore corrections
Change-Id: Ifdae16f0a401d28322796b3fecc2e575896fca37 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/405246 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent fca0324 commit 26f2206

File tree

3 files changed

+85
-11
lines changed

3 files changed

+85
-11
lines changed

pkg/analysis_server/test/src/services/correction/fix/remove_ignore_comment_test.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ class C {
7474
class C {
7575
void f(){}
7676
}
77+
''');
78+
}
79+
80+
Future<void> test_line_eol() async {
81+
await resolveTestCode('''
82+
class C {
83+
void f(){} // ignore: unused_local_variable
84+
}
85+
''');
86+
await assertHasFix('''
87+
class C {
88+
void f(){}
89+
}
7790
''');
7891
}
7992
}

pkg/analysis_server/test/src/services/correction/fix/remove_ignored_diagnostic_test.dart

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,30 @@ int f() => null;
9494
''');
9595
}
9696

97+
Future<void> test_file_first_eol() async {
98+
await resolveTestCode('''
99+
import 'dart:io'; // ignore_for_file: unused_local_variable, unused_import
100+
void f() {}
101+
''');
102+
await assertHasFix('''
103+
import 'dart:io'; // ignore_for_file: unused_import
104+
void f() {}
105+
''');
106+
}
107+
108+
Future<void> test_file_first_leading_and_eol() async {
109+
await resolveTestCode('''
110+
// ignore_for_file: return_of_invalid_type
111+
import 'dart:io'; // ignore_for_file: unused_local_variable, unused_import
112+
int f() => null;
113+
''');
114+
await assertHasFix('''
115+
// ignore_for_file: return_of_invalid_type
116+
import 'dart:io'; // ignore_for_file: unused_import
117+
int f() => null;
118+
''');
119+
}
120+
97121
Future<void> test_file_last() async {
98122
await resolveTestCode('''
99123
// ignore_for_file: return_of_invalid_type, unused_local_variable
@@ -127,6 +151,30 @@ int f() => null;
127151
''');
128152
}
129153

154+
Future<void> test_line_first_eol() async {
155+
await resolveTestCode('''
156+
class C {
157+
int f() => null; // ignore: unused_local_variable, return_of_invalid_type
158+
}''');
159+
await assertHasFix('''
160+
class C {
161+
int f() => null; // ignore: return_of_invalid_type
162+
}''');
163+
}
164+
165+
Future<void> test_line_first_leading_and_eol() async {
166+
await resolveTestCode('''
167+
class C {
168+
// ignore: private_optional_parameter
169+
int f({required _a}) => null; // ignore: unused_local_variable, return_of_invalid_type
170+
}''');
171+
await assertHasFix('''
172+
class C {
173+
// ignore: private_optional_parameter
174+
int f({required _a}) => null; // ignore: return_of_invalid_type
175+
}''');
176+
}
177+
130178
Future<void> test_line_last() async {
131179
await resolveTestCode('''
132180
// ignore: return_of_invalid_type, unused_local_variable

pkg/analyzer/lib/src/error/ignore_validator.dart

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -215,21 +215,34 @@ class IgnoreValidator {
215215
}
216216
}
217217

218-
var diagnosticsOnLine = 0;
218+
// We need to calculate if there are multiple diagnostic names in this
219+
// ignore comment.
220+
//
221+
// (This will determine what kind of fix to propose.)
222+
219223
var currentLine = _lineInfo.getLocation(ignoredName.offset).lineNumber;
220224

221-
// Calculate if there are multiple diagnostic names in this ignore comment.
222-
// (This will determine what kind of fix to propose.)
225+
// First we need to collect the (possibly) relevant ignores.
226+
late Iterable<IgnoredElement> ignoredElements;
223227
if (forFile) {
224-
for (var ignore in _ignoreInfo.ignoredForFile) {
225-
if (ignore is IgnoredDiagnosticName) {
226-
var ignoreLine = _lineInfo.getLocation(ignore.offset).lineNumber;
227-
if (ignoreLine == currentLine) diagnosticsOnLine++;
228-
}
229-
}
228+
ignoredElements = _ignoreInfo.ignoredForFile;
230229
} else {
231-
diagnosticsOnLine =
232-
_ignoreInfo.ignoredOnLine[currentLine + 1]?.length ?? 0;
230+
// To account for preceding and same-line ignore comments, we look at
231+
// the current line and its next.
232+
ignoredElements = {
233+
...?_ignoreInfo.ignoredOnLine[currentLine],
234+
...?_ignoreInfo.ignoredOnLine[currentLine + 1],
235+
};
236+
}
237+
238+
// Then we further narrow them down to ignored diagnostics that correspond
239+
// to the [currentLine].
240+
var diagnosticsOnLine = 0;
241+
for (var ignore in ignoredElements) {
242+
if (ignore is IgnoredDiagnosticName) {
243+
var ignoreLine = _lineInfo.getLocation(ignore.offset).lineNumber;
244+
if (ignoreLine == currentLine) diagnosticsOnLine++;
245+
}
233246
}
234247

235248
late ErrorCode lintCode;

0 commit comments

Comments
 (0)