Skip to content

Commit 2dc6d31

Browse files
pqCommit Queue
authored andcommitted
[lint] new lint: unnecessary_ignore
See: #35234 As a follow-up, I'll update the reporting to skip generated files. Change-Id: Icddcdf59269d82576eefd810671a3e992f2bcca5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/404480 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent 927769e commit 2dc6d31

File tree

18 files changed

+265
-111
lines changed

18 files changed

+265
-111
lines changed

pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2421,6 +2421,10 @@ LintCode.unnecessary_final_without_type:
24212421
status: hasFix
24222422
LintCode.unnecessary_getters_setters:
24232423
status: hasFix
2424+
LintCode.unnecessary_ignore:
2425+
status: needsFix
2426+
notes: |-
2427+
Remove the ignore comment (or one code in the comment).
24242428
LintCode.unnecessary_lambdas:
24252429
status: hasFix
24262430
LintCode.unnecessary_late:
@@ -3751,10 +3755,6 @@ WarningCode.UNNECESSARY_CAST:
37513755
status: hasFix
37523756
WarningCode.UNNECESSARY_FINAL:
37533757
status: hasFix
3754-
WarningCode.UNNECESSARY_IGNORE:
3755-
status: needsFix
3756-
notes: |-
3757-
Remove the ignore comment (or one code in the comment).
37583758
WarningCode.UNNECESSARY_NAN_COMPARISON_FALSE:
37593759
status: hasFix
37603760
notes: |-

pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,16 +350,20 @@ class LibraryAnalyzer {
350350

351351
_checkForInconsistentLanguageVersionOverride();
352352

353+
var validateUnnecessaryIgnores =
354+
_analysisOptions.isLintEnabled('unnecessary_ignore');
355+
353356
// This must happen after all other diagnostics have been computed but
354357
// before the list of diagnostics has been filtered.
355358
for (var fileAnalysis in _libraryFiles.values) {
356359
IgnoreValidator(
357-
fileAnalysis.errorReporter,
358-
fileAnalysis.errorListener.errors,
359-
fileAnalysis.ignoreInfo,
360-
fileAnalysis.unit.lineInfo,
361-
_analysisOptions.unignorableNames,
362-
).reportErrors();
360+
fileAnalysis.errorReporter,
361+
fileAnalysis.errorListener.errors,
362+
fileAnalysis.ignoreInfo,
363+
fileAnalysis.unit.lineInfo,
364+
_analysisOptions.unignorableNames,
365+
validateUnnecessaryIgnores)
366+
.reportErrors();
363367
}
364368
}
365369

pkg/analyzer/lib/src/error/codes.g.dart

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7467,17 +7467,6 @@ class WarningCode extends ErrorCode {
74677467
hasPublishedDocs: true,
74687468
);
74697469

7470-
/// Parameters:
7471-
/// 0: the name of the diagnostic being ignored
7472-
static const WarningCode UNNECESSARY_IGNORE = WarningCode(
7473-
'UNNECESSARY_IGNORE',
7474-
"The diagnostic '{0}' isn't produced at this location so it doesn't need "
7475-
"to be ignored.",
7476-
correctionMessage:
7477-
"Try removing the name from the list, or removing the whole comment if "
7478-
"this is the only name in the list.",
7479-
);
7480-
74817470
/// No parameters.
74827471
static const WarningCode UNNECESSARY_NAN_COMPARISON_FALSE = WarningCode(
74837472
'UNNECESSARY_NAN_COMPARISON',

pkg/analyzer/lib/src/error/error_code_values.g.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,6 @@ const List<ErrorCode> errorCodeValues = [
10901090
WarningCode.UNNECESSARY_CAST,
10911091
WarningCode.UNNECESSARY_CAST_PATTERN,
10921092
WarningCode.UNNECESSARY_FINAL,
1093-
WarningCode.UNNECESSARY_IGNORE,
10941093
WarningCode.UNNECESSARY_NAN_COMPARISON_FALSE,
10951094
WarningCode.UNNECESSARY_NAN_COMPARISON_TRUE,
10961095
WarningCode.UNNECESSARY_NO_SUCH_METHOD,

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

Lines changed: 60 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,21 @@ import 'package:analyzer/error/listener.dart';
77
import 'package:analyzer/source/line_info.dart';
88
import 'package:analyzer/src/error/codes.dart';
99
import 'package:analyzer/src/ignore_comments/ignore_info.dart';
10+
import 'package:analyzer/src/lint/registry.dart';
11+
import 'package:analyzer/src/lint/state.dart';
1012

1113
/// Used to validate the ignore comments in a single file.
1214
class IgnoreValidator {
15+
/// A list of known error codes used to ensure we don't over-report
16+
/// `unnecessary_ignore`s on error codes that may be contributed by a plugin.
17+
static final Set<String> _validErrorCodeNames =
18+
errorCodeValues.map((e) => e.name.toLowerCase()).toSet();
19+
20+
/// The error code used to report `unnecessary_ignore`s.
21+
/// This code is set when the `UnnecessaryIgnore` lint rule is instantiated and
22+
/// registered by the linter.
23+
static late ErrorCode unnecessaryIgnoreLintCode;
24+
1325
/// The error reporter to which errors are to be reported.
1426
final ErrorReporter _errorReporter;
1527

@@ -28,11 +40,14 @@ class IgnoreValidator {
2840
/// use because we have no visibility of them here.
2941
final Set<String> _unignorableNames;
3042

43+
/// Whether to validate unnecessary ignores (enabled by the `unnecessary_ignore` lint).
44+
final bool _validateUnnecessaryIgnores;
45+
3146
/// Initialize a newly created validator to report any issues with ignore
3247
/// comments in the file being analyzed. The diagnostics will be reported to
3348
/// the [_errorReporter].
3449
IgnoreValidator(this._errorReporter, this._reportedErrors, this._ignoreInfo,
35-
this._lineInfo, this._unignorableNames);
50+
this._lineInfo, this._unignorableNames, this._validateUnnecessaryIgnores);
3651

3752
/// Report any issues with ignore comments in the file being analyzed.
3853
void reportErrors() {
@@ -124,7 +139,7 @@ class IgnoreValidator {
124139
// errorCode: WarningCode.UNIGNORABLE_IGNORE,
125140
// offset: unignorableName.offset,
126141
// length: name.length,
127-
// data: [name]);
142+
// arguments: [name]);
128143
// list.remove(unignorableName);
129144
// }
130145
// }
@@ -153,46 +168,49 @@ class IgnoreValidator {
153168
/// Report the [ignoredNames] as being unnecessary.
154169
void _reportUnnecessaryOrRemovedOrDeprecatedIgnores(
155170
List<IgnoredElement> ignoredNames) {
156-
// TODO(pq): find the right way to roll-out enablement and uncomment
157-
// https://github.com/dart-lang/sdk/issues/51214
158-
// for (var ignoredName in ignoredNames) {
159-
// if (ignoredName is IgnoredDiagnosticName) {
160-
// var name = ignoredName.name;
161-
// var rule = Registry.ruleRegistry.getRule(name);
162-
// if (rule != null) {
163-
// var state = rule.state;
164-
// var since = state.since.toString();
165-
// if (state is DeprecatedState) {
166-
// // `todo`(pq): implement
167-
// } else if (state is RemovedState) {
168-
// var replacedBy = state.replacedBy;
169-
// if (replacedBy != null) {
170-
// _errorReporter.atOffset(
171-
// errorCode: WarningCode.REPLACED_LINT_USE,
172-
// offset: ignoredName.offset,
173-
// length: name.length,
174-
// data: [name, since, replacedBy]);
175-
// continue;
176-
// } else {
177-
// _errorReporter.atOffset(
178-
// errorCode: WarningCode.REMOVED_LINT_USE,
179-
// offset: ignoredName.offset,
180-
// length: name.length,
181-
// data: [name, since]);
182-
// continue;
183-
// }
184-
// }
185-
// }
186-
187-
// // TODO(brianwilkerson): Uncomment the code below after the unnecessary
188-
// // ignores in the Flutter code base have been cleaned up.
189-
// _errorReporter.atOffset(
190-
// errorCode: WarningCode.UNNECESSARY_IGNORE,
191-
// offset: ignoredName.offset,
192-
// length: name.length,
193-
// data: [name]);
194-
// }
195-
// }
171+
if (!_validateUnnecessaryIgnores) return;
172+
173+
for (var ignoredName in ignoredNames) {
174+
if (ignoredName is IgnoredDiagnosticName) {
175+
var name = ignoredName.name;
176+
var rule = Registry.ruleRegistry.getRule(name);
177+
if (rule == null) {
178+
// If a code is not a lint or a recognized error,
179+
// don't report. (It could come from a plugin.)
180+
// TODO(pq): consider another diagnostic that reports undefined codes
181+
if (!_validErrorCodeNames.contains(name.toLowerCase())) continue;
182+
} else {
183+
var state = rule.state;
184+
var since = state.since.toString();
185+
if (state is DeprecatedState) {
186+
// `todo`(pq): implement
187+
} else if (state is RemovedState) {
188+
var replacedBy = state.replacedBy;
189+
if (replacedBy != null) {
190+
_errorReporter.atOffset(
191+
errorCode: WarningCode.REPLACED_LINT_USE,
192+
offset: ignoredName.offset,
193+
length: name.length,
194+
arguments: [name, since, replacedBy]);
195+
continue;
196+
} else {
197+
_errorReporter.atOffset(
198+
errorCode: WarningCode.REMOVED_LINT_USE,
199+
offset: ignoredName.offset,
200+
length: name.length,
201+
arguments: [name, since]);
202+
continue;
203+
}
204+
}
205+
}
206+
207+
_errorReporter.atOffset(
208+
errorCode: unnecessaryIgnoreLintCode,
209+
offset: ignoredName.offset,
210+
length: name.length,
211+
arguments: [name]);
212+
}
213+
}
196214
}
197215
}
198216

pkg/analyzer/messages.yaml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27022,13 +27022,6 @@ WarningCode:
2702227022
B(super.value);
2702327023
}
2702427024
```
27025-
UNNECESSARY_IGNORE:
27026-
problemMessage: "The diagnostic '{0}' isn't produced at this location so it doesn't need to be ignored."
27027-
correctionMessage: Try removing the name from the list, or removing the whole comment if this is the only name in the list.
27028-
hasPublishedDocs: false
27029-
comment: |-
27030-
Parameters:
27031-
0: the name of the diagnostic being ignored
2703227025
UNNECESSARY_NAN_COMPARISON_FALSE:
2703327026
sharedName: UNNECESSARY_NAN_COMPARISON
2703427027
problemMessage: A double can't equal 'double.nan', so the condition is always 'false'.

pkg/analyzer/test/src/diagnostics/test_all.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,6 @@ import 'unignorable_ignore_test.dart' as unignorable_ignore;
875875
import 'unnecessary_cast_pattern_test.dart' as unnecessary_cast_pattern;
876876
import 'unnecessary_cast_test.dart' as unnecessary_cast;
877877
import 'unnecessary_final_test.dart' as unnecessary_final;
878-
import 'unnecessary_ignore_test.dart' as unnecessary_ignore;
879878
import 'unnecessary_import_test.dart' as unnecessary_import;
880879
import 'unnecessary_nan_comparison_test.dart' as unnecessary_nan_comparison;
881880
import 'unnecessary_no_such_method_test.dart' as unnecessary_no_such_method;
@@ -1498,7 +1497,6 @@ main() {
14981497
unnecessary_cast_pattern.main();
14991498
unnecessary_cast.main();
15001499
unnecessary_final.main();
1501-
unnecessary_ignore.main();
15021500
unnecessary_nan_comparison.main();
15031501
unnecessary_no_such_method.main();
15041502
unnecessary_non_null_assertion.main();

pkg/analyzer/test/src/diagnostics/unnecessary_ignore_test.dart

Lines changed: 0 additions & 37 deletions
This file was deleted.

pkg/analyzer/tool/diagnostics/diagnostics.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28689,6 +28689,33 @@ class C {
2868928689
}
2869028690
```
2869128691

28692+
### unnecessary_ignore
28693+
28694+
_The diagnostic '{0}' isn't produced at this location so it doesn't need to be
28695+
ignored._
28696+
28697+
#### Description
28698+
28699+
The analyzer produces this diagnostic when an ignore is specified to ignore a diagnostic that isn't produced.
28700+
28701+
#### Example
28702+
28703+
The following code produces this diagnostic because the `unused_local_variable`
28704+
diagnostic isn't reported at the ignored location:
28705+
28706+
```dart
28707+
// ignore: [!unused_local_variable!]
28708+
void f() {}
28709+
```
28710+
28711+
#### Common fixes
28712+
28713+
Remove the ignore comment:
28714+
28715+
```dart
28716+
void f() {}
28717+
```
28718+
2869228719
### unnecessary_lambdas
2869328720

2869428721
_Closure should be a tearoff._

pkg/linter/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# 3.7.0-wip
1+
# 3.8.0-wip
2+
3+
- new _(experimental)_ lint: `unnecessary_ignore`
4+
5+
# 3.7.0
26

37
- new lint: `unnecessary_underscores`
48
- new lint: `strict_top_level_inference`

0 commit comments

Comments
 (0)