Skip to content

Hint to catch redundant //ignores #35234

Closed

Description

@Hixie

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.

cc @a14n

Activity

  1. bwilkerson commented on Nov 20, 2018

    @bwilkerson
    Member

    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.

  2. zoechi commented on Nov 21, 2018

    @zoechi
    Contributor

    dup of #57404

  3. bwilkerson commented on Nov 21, 2018

    @bwilkerson
    Member

    Because it can't be implemented as a lint, the linter issue has been closed.

  4. changed the title [-]Lint to catch redundant //ignores[/-] [+]Hint to catch redundant //ignores[/+] on Nov 29, 2018
  5. added
    P2A bug or feature request we're likely to work on
    type-enhancementA request for a change that isn't a bug
    on Nov 29, 2018
  6. srawlins commented on Nov 26, 2019

    @srawlins
    Member

    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: 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.

  7. bwilkerson commented on Nov 26, 2019

    @bwilkerson
    Member

    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.

  8. srawlins commented on Nov 26, 2019

    @srawlins
    Member

    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.

  9. srawlins commented on Nov 30, 2019

    @srawlins
    Member

    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

    // ignore: avoid_unused_constructor_parameters, the parameter is deprecated.
    

    I guess at a minimum I would ignore "ignored codes" with whitespace (don't call them out as unused). Also I've seen this:

    // ignore: prefer_void_to_null, https://github.com/dart-lang/sdk/issues/34416
    

    So... ignore "ignored codes" that... are not valid Dart identifiers?

  10. bwilkerson commented on Nov 30, 2019

    @bwilkerson
    Member

    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"?

  11. srawlins commented on Dec 1, 2019

    @srawlins
    Member
  12. srawlins commented on Dec 1, 2019

    @srawlins
    Member

    @Hixie how will Flutter deal with "unused" ignore comments and Flutter's dev/analyze/bots.dart's verifyDeprecations?

    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=327666716

  13. srawlins commented on Dec 2, 2019

    @srawlins
    Member

    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:?

  14. 42 remaining items

  15. srawlins commented on Jan 13, 2025

    @srawlins
    Member

    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_seven
    int x = 7;

    Don't ask me what this looks like if you also have the document_ignores rule enabled.

  16. rrousselGit commented on Jan 13, 2025

    @rrousselGit

    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

  17. rrousselGit commented on Jan 13, 2025

    @rrousselGit

    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_seven
    int 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>

    Although that's probably a lot for not much.

  18. pq commented on Jan 14, 2025

    @pq
    Member

    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?

  19. rrousselGit commented on Jan 14, 2025

    @rrousselGit

    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...

  20. added a commit that references this issue on Jan 20, 2025
  21. pq commented on Feb 5, 2025

    @pq
    Member

    Thanks again for all the feedback. A basic implementation of an unnecessary_ignore lint has been landed w/ associated fixes.

    Feel free to open new issues w/ any issues (and cc me).

    Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P2A bug or feature request we're likely to work ondevexp-uxdevexp-warningIssues with the analyzer's Warning codeslegacy-area-analyzerUse area-devexp instead.type-enhancementA request for a change that isn't a bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions