Skip to content

Commit

Permalink
[lint] new unnecessary_ignore error codes
Browse files Browse the repository at this point in the history
(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]>
  • Loading branch information
pq authored and Commit Queue committed Jan 15, 2025
1 parent b6cc8a7 commit fa06595
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2425,6 +2425,12 @@ LintCode.unnecessary_ignore:
status: needsFix
notes: |-
Remove the ignore comment (or one code in the comment).
LintCode.unnecessary_ignore_file:
status: needsFix
LintCode.unnecessary_ignore_name:
status: needsFix
LintCode.unnecessary_ignore_name_file:
status: needsFix
LintCode.unnecessary_lambdas:
status: hasFix
LintCode.unnecessary_late:
Expand Down
28 changes: 22 additions & 6 deletions pkg/analyzer/lib/src/error/ignore_validator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ class IgnoreValidator {
static final Set<String> _validErrorCodeNames =
errorCodeValues.map((e) => e.name.toLowerCase()).toSet();

/// The error code used to report `unnecessary_ignore`s.
/// This code is set when the `UnnecessaryIgnore` lint rule is instantiated and
/// Error codes used to report `unnecessary_ignore`s.
/// These codes are set when the `UnnecessaryIgnore` lint rule is instantiated and
/// registered by the linter.
static late ErrorCode unnecessaryIgnoreLintCode;
static late ErrorCode unnecessaryIgnoreLocationLintCode;
static late ErrorCode unnecessaryIgnoreFileLintCode;
static late ErrorCode unnecessaryIgnoreNameLocationLintCode;
static late ErrorCode unnecessaryIgnoreNameFileLintCode;

/// The error reporter to which errors are to be reported.
final ErrorReporter _errorReporter;
Expand Down Expand Up @@ -120,7 +123,8 @@ class IgnoreValidator {
//
// Report any remaining ignored names as being unnecessary.
//
_reportUnnecessaryOrRemovedOrDeprecatedIgnores(ignoredForFile);
_reportUnnecessaryOrRemovedOrDeprecatedIgnores(ignoredForFile,
forFile: true);
for (var ignoredOnLine in ignoredOnLineMap.values) {
_reportUnnecessaryOrRemovedOrDeprecatedIgnores(ignoredOnLine);
}
Expand Down Expand Up @@ -167,7 +171,8 @@ class IgnoreValidator {

/// Report the [ignoredNames] as being unnecessary.
void _reportUnnecessaryOrRemovedOrDeprecatedIgnores(
List<IgnoredElement> ignoredNames) {
List<IgnoredElement> ignoredNames,
{bool forFile = false}) {
if (!_validateUnnecessaryIgnores) return;

for (var ignoredName in ignoredNames) {
Expand Down Expand Up @@ -204,8 +209,19 @@ class IgnoreValidator {
}
}

late ErrorCode lintCode;
if (ignoredNames.length > 1) {
lintCode = forFile
? unnecessaryIgnoreNameFileLintCode
: unnecessaryIgnoreNameLocationLintCode;
} else {
lintCode = forFile
? unnecessaryIgnoreFileLintCode
: unnecessaryIgnoreLocationLintCode;
}

_errorReporter.atOffset(
errorCode: unnecessaryIgnoreLintCode,
errorCode: lintCode,
offset: ignoredName.offset,
length: name.length,
arguments: [name]);
Expand Down
3 changes: 3 additions & 0 deletions pkg/analyzer/tool/diagnostics/diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -28694,6 +28694,9 @@ class C {
_The diagnostic '{0}' isn't produced at this location so it doesn't need to be
ignored._

_The diagnostic '{0}' isn't produced in this file so it doesn't need to be
ignored._

#### Description

The analyzer produces this diagnostic when an ignore is specified to ignore a diagnostic that isn't produced.
Expand Down
28 changes: 25 additions & 3 deletions pkg/linter/lib/src/lint_codes.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1640,12 +1640,34 @@ class LinterLintCode extends LintCode {
LintNames.unnecessary_ignore,
"The diagnostic '{0}' isn't produced at this location so it doesn't need "
"to be ignored.",
correctionMessage:
"Try removing the name from the list, or removing the whole comment if "
"this is the only name in the list.",
correctionMessage: "Try removing the ignore comment.",
uniqueName: 'unnecessary_ignore',
);

static const LintCode unnecessary_ignore_file = LinterLintCode(
LintNames.unnecessary_ignore,
"The diagnostic '{0}' isn't produced in this file so it doesn't need to be "
"ignored.",
correctionMessage: "Try removing the ignore comment.",
uniqueName: 'unnecessary_ignore_file',
);

static const LintCode unnecessary_ignore_name = LinterLintCode(
LintNames.unnecessary_ignore,
"The diagnostic '{0}' isn't produced at this location so it doesn't need "
"to be ignored.",
correctionMessage: "Try removing the name from the list.",
uniqueName: 'unnecessary_ignore_name',
);

static const LintCode unnecessary_ignore_name_file = LinterLintCode(
LintNames.unnecessary_ignore,
"The diagnostic '{0}' isn't produced in this file so it doesn't need to be "
"ignored.",
correctionMessage: "Try removing the name from the list.",
uniqueName: 'unnecessary_ignore_name_file',
);

static const LintCode unnecessary_lambdas = LinterLintCode(
LintNames.unnecessary_lambdas,
"Closure should be a tearoff.",
Expand Down
18 changes: 15 additions & 3 deletions pkg/linter/lib/src/rules/unnecessary_ignore.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,26 @@ const _desc = r"Don't ignore a diagnostic code that is not produced.";

class UnnecessaryIgnore extends LintRule {
UnnecessaryIgnore() : super(name: 'unnecessary_ignore', description: _desc) {
// Register the unnecessary_ignore lint code with the analyzer's validator.
// Register the unnecessary_ignore lint codes with the analyzer's validator.
// We do this here to avoid having to introduce a dependency from the analyzer
// on the linter.
IgnoreValidator.unnecessaryIgnoreLintCode = lintCode;
IgnoreValidator.unnecessaryIgnoreFileLintCode =
LinterLintCode.unnecessary_ignore_file;
IgnoreValidator.unnecessaryIgnoreLocationLintCode =
LinterLintCode.unnecessary_ignore;
IgnoreValidator.unnecessaryIgnoreNameFileLintCode =
LinterLintCode.unnecessary_ignore_name_file;
IgnoreValidator.unnecessaryIgnoreNameLocationLintCode =
LinterLintCode.unnecessary_ignore_name;
}

@override
LintCode get lintCode => LinterLintCode.unnecessary_ignore;
List<LintCode> get lintCodes => const [
LinterLintCode.unnecessary_ignore,
LinterLintCode.unnecessary_ignore_file,
LinterLintCode.unnecessary_ignore_name,
LinterLintCode.unnecessary_ignore_name_file,
];

/// Note that there is intentionally no registration logic as there is no visiting or
/// analysis done in the lint implementation. Instead the heavy-lifting is done in an
Expand Down
17 changes: 16 additions & 1 deletion pkg/linter/messages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12057,7 +12057,7 @@ LintCode:
unnecessary_ignore:
sharedName: unnecessary_ignore
problemMessage: "The diagnostic '{0}' isn't produced at this location so it doesn't need to be ignored."
correctionMessage: "Try removing the name from the list, or removing the whole comment if this is the only name in the list."
correctionMessage: "Try removing the ignore comment."
state:
experimental: "3.8"
categories: [style]
Expand Down Expand Up @@ -12086,6 +12086,21 @@ LintCode:
```
deprecatedDetails: |-
**DON'T** specify an ignore for a diagnostic that is not produced.
unnecessary_ignore_file:
sharedName: unnecessary_ignore
problemMessage: "The diagnostic '{0}' isn't produced in this file so it doesn't need to be ignored."
correctionMessage: "Try removing the ignore comment."
hasPublishedDocs: false
unnecessary_ignore_name:
sharedName: unnecessary_ignore
problemMessage: "The diagnostic '{0}' isn't produced at this location so it doesn't need to be ignored."
correctionMessage: "Try removing the name from the list."
hasPublishedDocs: false
unnecessary_ignore_name_file:
sharedName: unnecessary_ignore
problemMessage: "The diagnostic '{0}' isn't produced in this file so it doesn't need to be ignored."
correctionMessage: "Try removing the name from the list."
hasPublishedDocs: false
unnecessary_lambdas:
problemMessage: "Closure should be a tearoff."
correctionMessage: "Try using a tearoff rather than a closure."
Expand Down

0 comments on commit fa06595

Please sign in to comment.