Skip to content

Commit fa06595

Browse files
pqCommit Queue
authored andcommitted
[lint] new unnecessary_ignore error codes
(Fixes to follow) Bug: #35234 Change-Id: I7a9255ffba7d9b0dc5e3ce9783f8f4ba63d8c854 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/404701 Commit-Queue: Phil Quitslund <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent b6cc8a7 commit fa06595

File tree

6 files changed

+87
-13
lines changed

6 files changed

+87
-13
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2425,6 +2425,12 @@ LintCode.unnecessary_ignore:
24252425
status: needsFix
24262426
notes: |-
24272427
Remove the ignore comment (or one code in the comment).
2428+
LintCode.unnecessary_ignore_file:
2429+
status: needsFix
2430+
LintCode.unnecessary_ignore_name:
2431+
status: needsFix
2432+
LintCode.unnecessary_ignore_name_file:
2433+
status: needsFix
24282434
LintCode.unnecessary_lambdas:
24292435
status: hasFix
24302436
LintCode.unnecessary_late:

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ class IgnoreValidator {
1717
static final Set<String> _validErrorCodeNames =
1818
errorCodeValues.map((e) => e.name.toLowerCase()).toSet();
1919

20-
/// The error code used to report `unnecessary_ignore`s.
21-
/// This code is set when the `UnnecessaryIgnore` lint rule is instantiated and
20+
/// Error codes used to report `unnecessary_ignore`s.
21+
/// These codes are set when the `UnnecessaryIgnore` lint rule is instantiated and
2222
/// registered by the linter.
23-
static late ErrorCode unnecessaryIgnoreLintCode;
23+
static late ErrorCode unnecessaryIgnoreLocationLintCode;
24+
static late ErrorCode unnecessaryIgnoreFileLintCode;
25+
static late ErrorCode unnecessaryIgnoreNameLocationLintCode;
26+
static late ErrorCode unnecessaryIgnoreNameFileLintCode;
2427

2528
/// The error reporter to which errors are to be reported.
2629
final ErrorReporter _errorReporter;
@@ -120,7 +123,8 @@ class IgnoreValidator {
120123
//
121124
// Report any remaining ignored names as being unnecessary.
122125
//
123-
_reportUnnecessaryOrRemovedOrDeprecatedIgnores(ignoredForFile);
126+
_reportUnnecessaryOrRemovedOrDeprecatedIgnores(ignoredForFile,
127+
forFile: true);
124128
for (var ignoredOnLine in ignoredOnLineMap.values) {
125129
_reportUnnecessaryOrRemovedOrDeprecatedIgnores(ignoredOnLine);
126130
}
@@ -167,7 +171,8 @@ class IgnoreValidator {
167171

168172
/// Report the [ignoredNames] as being unnecessary.
169173
void _reportUnnecessaryOrRemovedOrDeprecatedIgnores(
170-
List<IgnoredElement> ignoredNames) {
174+
List<IgnoredElement> ignoredNames,
175+
{bool forFile = false}) {
171176
if (!_validateUnnecessaryIgnores) return;
172177

173178
for (var ignoredName in ignoredNames) {
@@ -204,8 +209,19 @@ class IgnoreValidator {
204209
}
205210
}
206211

212+
late ErrorCode lintCode;
213+
if (ignoredNames.length > 1) {
214+
lintCode = forFile
215+
? unnecessaryIgnoreNameFileLintCode
216+
: unnecessaryIgnoreNameLocationLintCode;
217+
} else {
218+
lintCode = forFile
219+
? unnecessaryIgnoreFileLintCode
220+
: unnecessaryIgnoreLocationLintCode;
221+
}
222+
207223
_errorReporter.atOffset(
208-
errorCode: unnecessaryIgnoreLintCode,
224+
errorCode: lintCode,
209225
offset: ignoredName.offset,
210226
length: name.length,
211227
arguments: [name]);

pkg/analyzer/tool/diagnostics/diagnostics.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28694,6 +28694,9 @@ class C {
2869428694
_The diagnostic '{0}' isn't produced at this location so it doesn't need to be
2869528695
ignored._
2869628696

28697+
_The diagnostic '{0}' isn't produced in this file so it doesn't need to be
28698+
ignored._
28699+
2869728700
#### Description
2869828701

2869928702
The analyzer produces this diagnostic when an ignore is specified to ignore a diagnostic that isn't produced.

pkg/linter/lib/src/lint_codes.g.dart

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,12 +1640,34 @@ class LinterLintCode extends LintCode {
16401640
LintNames.unnecessary_ignore,
16411641
"The diagnostic '{0}' isn't produced at this location so it doesn't need "
16421642
"to be ignored.",
1643-
correctionMessage:
1644-
"Try removing the name from the list, or removing the whole comment if "
1645-
"this is the only name in the list.",
1643+
correctionMessage: "Try removing the ignore comment.",
16461644
uniqueName: 'unnecessary_ignore',
16471645
);
16481646

1647+
static const LintCode unnecessary_ignore_file = LinterLintCode(
1648+
LintNames.unnecessary_ignore,
1649+
"The diagnostic '{0}' isn't produced in this file so it doesn't need to be "
1650+
"ignored.",
1651+
correctionMessage: "Try removing the ignore comment.",
1652+
uniqueName: 'unnecessary_ignore_file',
1653+
);
1654+
1655+
static const LintCode unnecessary_ignore_name = LinterLintCode(
1656+
LintNames.unnecessary_ignore,
1657+
"The diagnostic '{0}' isn't produced at this location so it doesn't need "
1658+
"to be ignored.",
1659+
correctionMessage: "Try removing the name from the list.",
1660+
uniqueName: 'unnecessary_ignore_name',
1661+
);
1662+
1663+
static const LintCode unnecessary_ignore_name_file = LinterLintCode(
1664+
LintNames.unnecessary_ignore,
1665+
"The diagnostic '{0}' isn't produced in this file so it doesn't need to be "
1666+
"ignored.",
1667+
correctionMessage: "Try removing the name from the list.",
1668+
uniqueName: 'unnecessary_ignore_name_file',
1669+
);
1670+
16491671
static const LintCode unnecessary_lambdas = LinterLintCode(
16501672
LintNames.unnecessary_lambdas,
16511673
"Closure should be a tearoff.",

pkg/linter/lib/src/rules/unnecessary_ignore.dart

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,26 @@ const _desc = r"Don't ignore a diagnostic code that is not produced.";
1111

1212
class UnnecessaryIgnore extends LintRule {
1313
UnnecessaryIgnore() : super(name: 'unnecessary_ignore', description: _desc) {
14-
// Register the unnecessary_ignore lint code with the analyzer's validator.
14+
// Register the unnecessary_ignore lint codes with the analyzer's validator.
1515
// We do this here to avoid having to introduce a dependency from the analyzer
1616
// on the linter.
17-
IgnoreValidator.unnecessaryIgnoreLintCode = lintCode;
17+
IgnoreValidator.unnecessaryIgnoreFileLintCode =
18+
LinterLintCode.unnecessary_ignore_file;
19+
IgnoreValidator.unnecessaryIgnoreLocationLintCode =
20+
LinterLintCode.unnecessary_ignore;
21+
IgnoreValidator.unnecessaryIgnoreNameFileLintCode =
22+
LinterLintCode.unnecessary_ignore_name_file;
23+
IgnoreValidator.unnecessaryIgnoreNameLocationLintCode =
24+
LinterLintCode.unnecessary_ignore_name;
1825
}
1926

2027
@override
21-
LintCode get lintCode => LinterLintCode.unnecessary_ignore;
28+
List<LintCode> get lintCodes => const [
29+
LinterLintCode.unnecessary_ignore,
30+
LinterLintCode.unnecessary_ignore_file,
31+
LinterLintCode.unnecessary_ignore_name,
32+
LinterLintCode.unnecessary_ignore_name_file,
33+
];
2234

2335
/// Note that there is intentionally no registration logic as there is no visiting or
2436
/// analysis done in the lint implementation. Instead the heavy-lifting is done in an

pkg/linter/messages.yaml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12057,7 +12057,7 @@ LintCode:
1205712057
unnecessary_ignore:
1205812058
sharedName: unnecessary_ignore
1205912059
problemMessage: "The diagnostic '{0}' isn't produced at this location so it doesn't need to be ignored."
12060-
correctionMessage: "Try removing the name from the list, or removing the whole comment if this is the only name in the list."
12060+
correctionMessage: "Try removing the ignore comment."
1206112061
state:
1206212062
experimental: "3.8"
1206312063
categories: [style]
@@ -12086,6 +12086,21 @@ LintCode:
1208612086
```
1208712087
deprecatedDetails: |-
1208812088
**DON'T** specify an ignore for a diagnostic that is not produced.
12089+
unnecessary_ignore_file:
12090+
sharedName: unnecessary_ignore
12091+
problemMessage: "The diagnostic '{0}' isn't produced in this file so it doesn't need to be ignored."
12092+
correctionMessage: "Try removing the ignore comment."
12093+
hasPublishedDocs: false
12094+
unnecessary_ignore_name:
12095+
sharedName: unnecessary_ignore
12096+
problemMessage: "The diagnostic '{0}' isn't produced at this location so it doesn't need to be ignored."
12097+
correctionMessage: "Try removing the name from the list."
12098+
hasPublishedDocs: false
12099+
unnecessary_ignore_name_file:
12100+
sharedName: unnecessary_ignore
12101+
problemMessage: "The diagnostic '{0}' isn't produced in this file so it doesn't need to be ignored."
12102+
correctionMessage: "Try removing the name from the list."
12103+
hasPublishedDocs: false
1208912104
unnecessary_lambdas:
1209012105
problemMessage: "Closure should be a tearoff."
1209112106
correctionMessage: "Try using a tearoff rather than a closure."

0 commit comments

Comments
 (0)