You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It'd be great if there was a lint that would trigger whenever there was an // ignore that wasn't actively silencing an analyzer message. This would let us catch old ignores that work around bugs that have since been fixed, or ignores that we don't need because we disabled the relevant lint, etc.
Reacted by Michael Goderbauer, Kathy Walrath, Simon Binder, Sam Rawlins, Günter Zöchbauer, Roman Petrov, Phil Quitslund, jellynoone, DetachHead, KotlinIsland and 4 more
I completely agree that we need this. It might even be reasonable to make it be opt-in.
However, we can't implement it as a normal lint because there's no way to guarantee that all of the other lints will have already been run. This would need to run after all other diagnostics have been generated, otherwise we won't be able to determine whether a given ignore is valid.
A new error code (a HintCode) called something like unused_ignore_comment.
This error code cannot be ignored with an ignore: or ignore_for_file: comment. It can be ignored via analysis_options.yaml.
Only unused ignore comments for error codes which exist are reported. For example, a typo like // ignore: daed_code does not ignore any real error, but no unused_ignore_comment Hint will be reported, since there exists no error code, daed_code.
This new error code is not intended to catch typos. It is intended to catch ignore comments which once were used, and are no longer.
If we did report comments like // ignore: to disregard and // Some things I wish I could\n// ignore: dead code for example, I think users would be fighting a lot of unintentional "ignore comments." Which, as stated above, themselves cannot be ignored from within the source file.
This error code cannot be ignored with an ignore: or ignore_for_file: comment.
I don't see any value in adding this special case handling. I think the number of people who will choose to ignore this hint will be very small and I don't see why we'd disallow it. Besides, if we want a way to make certain diagnostics impossible to ignore then that mechanism would cover this case without special casing.
... I think users would be fighting a lot of unintentional "ignore comments."
I doubt that's true, but we could gather evidence easily enough.
As a counter argument, though, I'll note that we recently removed a diagnostic (because the error was removed from the spec). If there are any ignore comments for that diagnostic, your proposed limitation would prevent this lint from helping users clean up their code. I think we could safely not generate diagnostics for comments that are not of the right form, but I'd prefer for the hint to work if what follows the ignore: is a comma separated list of identifiers. That should cover the examples you gave while still allowing it to catch errors that have been removed.
I don't see any value in adding this special case handling. I think the number of people who will choose to ignore this hint will be very small and I don't see why we'd disallow it.
I thought it would be a lot of effort to allow ignoring these, but I think I was wrong. These should be ignorable.
The way analyzer processes ignore comments is to take everything following ignore:, split it on commas, trim off white space at the beginning and end, and convert to lower case. That gives it a list of codes to ignore. This hint should process them the same way, and should share as much code as possible with IgnoreInfo in order to ensure consistency should that we change the way the comments are processed.
Every valid code name is a valid identifier. In both of the examples you gave we'd end up with items in the list that are not valid identifiers. I suspect we'll get the best results by discarding / ignoring anything that isn't a valid identifier, and then validating that each remaining identifier is a known code and that a diagnostic with that code is actually ignored at that location.
That hint processing will need to happen after all of the diagnostics have been computed, including lints, in order to know whether a diagnostic is reported at that location. Note that that means that it might be harder than I'd first assumed to make this hint be ignorable. If that's the case, then I don't think we should do the extra work until we know of a valid use case for allowing them to be ignored.
A new error code (a HintCode) called something like unused_ignore_comment.
I'm not sold on the name because of the fact that a single comment can contain both valid (used) and invalid (unused) codes. In that case the comment isn't unused, just one of the codes. Maybe "code" instead of "comment"?
I'm assuming that plugins get their own lint registry that's run separately from the "core lints" (in their own isolate). If that's right we'll have to (minimally) make sure we don't over-report... Thoughts?
Correct. The current plan is that ignoring plugin diagnostics inline (// ignore: and // ignore_for_file:) will require namespacing. Something like // ignore: plugin_1.diagnostic_name.
So the unnecessary ignore rule should probably ignore... anything that's not a simple identifier?
As for SDK versions, the only solution I've thought of is more ignores :P
// Some people are using Dart 4.3, where we have the "dont_use_seven" rule.// And some are using Dart 4.2. So we have to ignore unnecessary_ignore, in case// the active SDK is not aware of the "dont_use_seven" rule.// ignore: unnecessary_ignore// ignore: dont_use_sevenint x =7;
Don't ask me what this looks like if you also have the document_ignores rule enabled.
Correct. The current plan is that ignoring plugin diagnostics inline (// ignore: and // ignore_for_file:) will require namespacing. Something like // ignore: plugin_1.diagnostic_name.
So the unnecessary ignore rule should probably ignore... anything that's not a simple identifier?
Depending on how new plugins work, they could be required to list all Error Codes.
Then, analyzer could know if ignore: code matches something from a plugin or whether it's just useless given the current environment
As for SDK versions, the only solution I've thought of is more ignores :P
// Some people are using Dart 4.3, where we have the "dont_use_seven" rule.// And some are using Dart 4.2. So we have to ignore unnecessary_ignore, in case// the active SDK is not aware of the "dont_use_seven" rule.// ignore: unnecessary_ignore// ignore: dont_use_sevenint x =7;
I wonder if we could use some form of version constraint in the ignore to have them SDK/package aware
Like:
// ignore: code @SDK=^3.1.0<some code that warns after 3.1.0>
Depending on how new plugins work, they could be required to list all Error Codes.
Or maybe the plugin isolate could communicate a list of registered lints back to the main analyzer isolate when lints are added to the plugin's lint registry?
EDIT: Or maybe it's simpler.
If I understand the concern correctly, and we just don't want to overreport on diagnostics that are not produced by the main dart analyzer isolate but may get contributed by a plugin, I wonder if it's sufficient to only report on error codes that the dart analyze isolate knows about. In other words, if a code is not contributed by the analyzer or linter we skip it.
Looking ahead, it would be really nice to handle the plugin case correctly though and report unnecessary ignores for plugin-contributed diagnostics as well. Maybe the plugin isolate should get it's own dedicated ignore validator?
Doing nothing for unknown codes is probably enough.
Plugins can reimplement this logic themselves if they need it. To begin with, // ignore: code doesn't natively work with plugins at the moment, so...
Activity
bwilkerson commentedon Nov 20, 2018
I completely agree that we need this. It might even be reasonable to make it be opt-in.
However, we can't implement it as a normal lint because there's no way to guarantee that all of the other lints will have already been run. This would need to run after all other diagnostics have been generated, otherwise we won't be able to determine whether a given ignore is valid.
zoechi commentedon Nov 21, 2018
dup of #57404
bwilkerson commentedon Nov 21, 2018
Because it can't be implemented as a lint, the linter issue has been closed.
[-]Lint to catch redundant //ignores[/-][+]Hint to catch redundant //ignores[/+]srawlins commentedon Nov 26, 2019
I propose the following static analysis rule:
A new error code (a HintCode) called something like
unused_ignore_comment.This error code cannot be ignored with an
ignore:orignore_for_file:comment. It can be ignored viaanalysis_options.yaml.Only unused ignore comments for error codes which exist are reported. For example, a typo like
// ignore: daed_codedoes not ignore any real error, but nounused_ignore_commentHint will be reported, since there exists no error code,daed_code.This new error code is not intended to catch typos. It is intended to catch ignore comments which once were used, and are no longer.
If we did report comments like
// ignore: to disregardand// Some things I wish I could\n// ignore: dead code for example, I think users would be fighting a lot of unintentional "ignore comments." Which, as stated above, themselves cannot be ignored from within the source file.bwilkerson commentedon Nov 26, 2019
I don't see any value in adding this special case handling. I think the number of people who will choose to ignore this hint will be very small and I don't see why we'd disallow it. Besides, if we want a way to make certain diagnostics impossible to ignore then that mechanism would cover this case without special casing.
I doubt that's true, but we could gather evidence easily enough.
As a counter argument, though, I'll note that we recently removed a diagnostic (because the error was removed from the spec). If there are any ignore comments for that diagnostic, your proposed limitation would prevent this lint from helping users clean up their code. I think we could safely not generate diagnostics for comments that are not of the right form, but I'd prefer for the hint to work if what follows the
ignore:is a comma separated list of identifiers. That should cover the examples you gave while still allowing it to catch errors that have been removed.srawlins commentedon Nov 26, 2019
I thought it would be a lot of effort to allow ignoring these, but I think I was wrong. These should be ignorable.
srawlins commentedon Nov 30, 2019
I see the flutter repo using the ignore syntax like this in a dozen places:
https://github.com/flutter/flutter/blob/b9a56b9f48462d897101284f5f57b8568ef683c6/packages/flutter/lib/src/cupertino/text_theme.dart#L122
I guess at a minimum I would ignore "ignored codes" with whitespace (don't call them out as unused). Also I've seen this:
So... ignore "ignored codes" that... are not valid Dart identifiers?
bwilkerson commentedon Nov 30, 2019
The way analyzer processes ignore comments is to take everything following
ignore:, split it on commas, trim off white space at the beginning and end, and convert to lower case. That gives it a list of codes to ignore. This hint should process them the same way, and should share as much code as possible withIgnoreInfoin order to ensure consistency should that we change the way the comments are processed.Every valid code name is a valid identifier. In both of the examples you gave we'd end up with items in the list that are not valid identifiers. I suspect we'll get the best results by discarding / ignoring anything that isn't a valid identifier, and then validating that each remaining identifier is a known code and that a diagnostic with that code is actually ignored at that location.
That hint processing will need to happen after all of the diagnostics have been computed, including lints, in order to know whether a diagnostic is reported at that location. Note that that means that it might be harder than I'd first assumed to make this hint be ignorable. If that's the case, then I don't think we should do the extra work until we know of a valid use case for allowing them to be ignored.
I'm not sold on the name because of the fact that a single comment can contain both valid (used) and invalid (unused) codes. In that case the comment isn't unused, just one of the codes. Maybe "code" instead of "comment"?
srawlins commentedon Dec 1, 2019
I've got a WIP if anyone is interested. https://dart-review.googlesource.com/c/sdk/+/126620
srawlins commentedon Dec 1, 2019
@Hixie how will Flutter deal with "unused" ignore comments and Flutter's
dev/analyze/bots.dart'sverifyDeprecations?https://github.com/flutter/flutter/blob/b9a56b9f48462d897101284f5f57b8568ef683c6/dev/bots/analyze.dart#L167
I have a PR to remove "unused" ignore comments in flutter/flutter, but the PR breaks on instances of
flutter_deprecation_syntax: https://github.com/flutter/flutter/pull/45855/checks?check_run_id=327666716srawlins commentedon Dec 2, 2019
Same question for
flutter_tools/test/general.shard/forbidden_imports_test.dart, which looks at source files for certain patterns, allowable via, e.g.ignore: dart_convert_import,ignore: package_path_import.Maybe just a new way to ignore these things?
// flutter_ignore:?// ignore_for_flutter:?42 remaining items
srawlins commentedon Jan 13, 2025
Correct. The current plan is that ignoring plugin diagnostics inline (
// ignore:and// ignore_for_file:) will require namespacing. Something like// ignore: plugin_1.diagnostic_name.So the unnecessary ignore rule should probably ignore... anything that's not a simple identifier?
As for SDK versions, the only solution I've thought of is more ignores :P
Don't ask me what this looks like if you also have the document_ignores rule enabled.
rrousselGit commentedon Jan 13, 2025
Depending on how new plugins work, they could be required to list all Error Codes.
Then, analyzer could know if
ignore: codematches something from a plugin or whether it's just useless given the current environmentrrousselGit commentedon Jan 13, 2025
I wonder if we could use some form of version constraint in the ignore to have them SDK/package aware
Like:
Although that's probably a lot for not much.
pq commentedon Jan 14, 2025
Or maybe the plugin isolate could communicate a list of registered lints back to the main analyzer isolate when lints are added to the plugin's lint registry?
EDIT: Or maybe it's simpler.
If I understand the concern correctly, and we just don't want to overreport on diagnostics that are not produced by the main dart analyzer isolate but may get contributed by a plugin, I wonder if it's sufficient to only report on error codes that the dart analyze isolate knows about. In other words, if a code is not contributed by the analyzer or linter we skip it.
Looking ahead, it would be really nice to handle the plugin case correctly though and report unnecessary ignores for plugin-contributed diagnostics as well. Maybe the plugin isolate should get it's own dedicated ignore validator?
rrousselGit commentedon Jan 14, 2025
Doing nothing for unknown codes is probably enough.
Plugins can reimplement this logic themselves if they need it. To begin with,
// ignore: codedoesn't natively work with plugins at the moment, so...[CQ] cleanup some unnecessary ignores
[lint] new lint: `unnecessary_ignore`
[lint] `unecessary_ignore`: skip generated files
[lint] new `unnecessary_ignore` error codes
feat: unnecessary_ignore
pq commentedon Feb 5, 2025
Thanks again for all the feedback. A basic implementation of an
unnecessary_ignorelint has been landed w/ associated fixes.Feel free to open new issues w/ any issues (and cc me).
Thanks!
feat: unnecessary_ignore
feat: unnecessary_ignore
feat: unnecessary_ignore