Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Analyzer shows warning of use_late_for_private_fields_and_variables in wrong file when part and part of are used #2921

Closed
Tracked by #3965
Quijx opened this issue Sep 1, 2021 · 3 comments
Assignees
Labels
false-negative false-positive P3 A lower priority bug or feature request set-none Does not affect any rules in a defined rule set (e.g., core, recommended, flutter) type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@Quijx
Copy link

Quijx commented Sep 1, 2021

The analyzer shows a warning of the use_late_for_private_fields_and_variables lint in wrong file when part and part of are used.

Steps to reproduce

  1. Create a new dart project with dart create use_late_bug
  2. Add the following to the analysis_options.yaml file:
linter:
  rules:
    - use_late_for_private_fields_and_variables
  1. Create a file foo.dart in bin with the content:
part 'bar.dart';

class Foo {
  final String? _s;

  Foo(this._s);
}
  1. Create a file bar.dart in bin with the content:
part of 'foo.dart';

void bar() {
  print('bar');
}
  1. Run dart analyze. The output is:
Analyzing use_late_bug...              0.7s

   info • bin/bar.dart:4:13 • Use late for private members with non-nullable type. • use_late_for_private_fields_and_variables
   info • bin/foo.dart:4:17 • The value of the field '_s' isn't used. Try removing the field, or using it. • unused_field

2 issues found.

Expected Result

The warning for the lint use_late_for_private_fields_and_variables should be shown in the file foo.dart on the field _s.

Actual Result

The warning for the lint use_late_for_private_fields_and_variables is shown in the file bar.dart by the print.
Notably the warning is shown at index 46 of the bar.dart file and the field _s is also defined at index 46 in a string containing the foo.dart file.
So the warning is shown at the correct place, just in the wrong file.


Output of dart --version: Dart SDK version: 2.14.0-301.0.dev (dev) (Sat Jul 10 17:56:59 2021 -0700) on "linux_x64"

@bwilkerson bwilkerson transferred this issue from dart-lang/sdk Sep 1, 2021
@pq pq added set-none Does not affect any rules in a defined rule set (e.g., core, recommended, flutter) false-positive type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Sep 1, 2021
@pq pq added the P3 A lower priority bug or feature request label Nov 14, 2022
@pq
Copy link
Member

pq commented Jul 20, 2023

FWIW, the issue here is that today we can only report lints on the file currently being analyzed. To fix this we'll need to take a performance hit and do a fresh (and redundant) run through all the constituent part files that define a library. For an example of how this is done, see PreferFinalFields.

/fyi @a14n

@FMorschel
Copy link
Contributor

I'm not sure if this helps in any way for this issue to be solved, but I have a Flutter package and my latest commit is showing something similar. Here is the link to the latest commit where this is showing on my size_extension.dart file.

@srawlins srawlins self-assigned this May 15, 2024
copybara-service bot pushed a commit to dart-lang/sdk that referenced this issue May 17, 2024
This primarily takes the form of adding an ErrorReporter field to
LinterContextUnit. In addition, we make the getter access to
`Linter.reporter` protected, to prevent lint rule authors from trying
to call `rule.reporter.reportError` with a temporary AnalysisError
that attempts to point to a specific source. The _setter_ for
`Linter.reporter` remains public, as it is generally set by the
framework.

Some other changes are made in order to facilitate this:

* Remove LinterAnalysis.linterUnit, no longer needed.
* Remove LinterContextUnit2, no longer needed.
* Correct the tests for avoid_private_typedef_functions and
  use_late_for_private_fields_and_variables. These tests did not
  correctly test declarations of typedefs or variables _in_ part
  files.

Fixes dart-lang/linter#4946
Fixes dart-lang/linter#2921

Change-Id: I77c14ab6e56f369417a27f5f5227d6ec11486c3f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/366405
Commit-Queue: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
@srawlins
Copy link
Member

Finally fixed with dart-lang/sdk@bca7663 ; thanks for the repros.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
false-negative false-positive P3 A lower priority bug or feature request set-none Does not affect any rules in a defined rule set (e.g., core, recommended, flutter) type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

4 participants