From fa065954e58adfd63506e93ffc1c00c43ca3a723 Mon Sep 17 00:00:00 2001 From: pq Date: Wed, 15 Jan 2025 13:37:13 -0800 Subject: [PATCH] [lint] new `unnecessary_ignore` error codes (Fixes to follow) Bug: https://github.com/dart-lang/sdk/issues/35234 Change-Id: I7a9255ffba7d9b0dc5e3ce9783f8f4ba63d8c854 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/404701 Commit-Queue: Phil Quitslund Reviewed-by: Brian Wilkerson --- .../services/correction/error_fix_status.yaml | 6 ++++ .../lib/src/error/ignore_validator.dart | 28 +++++++++++++++---- pkg/analyzer/tool/diagnostics/diagnostics.md | 3 ++ pkg/linter/lib/src/lint_codes.g.dart | 28 +++++++++++++++++-- .../lib/src/rules/unnecessary_ignore.dart | 18 ++++++++++-- pkg/linter/messages.yaml | 17 ++++++++++- 6 files changed, 87 insertions(+), 13 deletions(-) diff --git a/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml b/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml index 0f46464bee08..478783c3e33d 100644 --- a/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml +++ b/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml @@ -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: diff --git a/pkg/analyzer/lib/src/error/ignore_validator.dart b/pkg/analyzer/lib/src/error/ignore_validator.dart index 01278aa76d05..1a48304919fc 100644 --- a/pkg/analyzer/lib/src/error/ignore_validator.dart +++ b/pkg/analyzer/lib/src/error/ignore_validator.dart @@ -17,10 +17,13 @@ class IgnoreValidator { static final Set _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; @@ -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); } @@ -167,7 +171,8 @@ class IgnoreValidator { /// Report the [ignoredNames] as being unnecessary. void _reportUnnecessaryOrRemovedOrDeprecatedIgnores( - List ignoredNames) { + List ignoredNames, + {bool forFile = false}) { if (!_validateUnnecessaryIgnores) return; for (var ignoredName in ignoredNames) { @@ -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]); diff --git a/pkg/analyzer/tool/diagnostics/diagnostics.md b/pkg/analyzer/tool/diagnostics/diagnostics.md index 164af2b53142..3855fe6ff0fb 100644 --- a/pkg/analyzer/tool/diagnostics/diagnostics.md +++ b/pkg/analyzer/tool/diagnostics/diagnostics.md @@ -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. diff --git a/pkg/linter/lib/src/lint_codes.g.dart b/pkg/linter/lib/src/lint_codes.g.dart index 8df605b1a310..5c43a1d40fd4 100644 --- a/pkg/linter/lib/src/lint_codes.g.dart +++ b/pkg/linter/lib/src/lint_codes.g.dart @@ -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.", diff --git a/pkg/linter/lib/src/rules/unnecessary_ignore.dart b/pkg/linter/lib/src/rules/unnecessary_ignore.dart index 09eba3367d59..34f05e435890 100644 --- a/pkg/linter/lib/src/rules/unnecessary_ignore.dart +++ b/pkg/linter/lib/src/rules/unnecessary_ignore.dart @@ -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 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 diff --git a/pkg/linter/messages.yaml b/pkg/linter/messages.yaml index e4453422778c..91e0b7f9dd74 100644 --- a/pkg/linter/messages.yaml +++ b/pkg/linter/messages.yaml @@ -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] @@ -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."