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 doesn't prompt quick fix to import for extension in null-aware access #56328

Closed
ChaserVasya opened this issue Jul 28, 2024 · 4 comments
Labels
analyzer-quick-fix area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P3 A lower priority bug or feature request type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@ChaserVasya
Copy link

extension.dart

extension MapT<T extends Object> on T {
  Output map<Output>(Output Function(T) mapper) => mapper(this);
}

main.dart

void main(){
    String? str = null;
    str?.map(print);
}

Analyzer doesn't find quick-fix for extension importing:
Снимок экрана 2024-07-28 в 20 08 50

But quick-fix is exists: import adding.
main.dart

import 'extension.dart';

void main() {
  String? str = null;
  str?.map(print);
}

#Desired behavior:
Quick-fix shows import extension.dart

fvm dart info

General info

  • Dart 3.2.3 (stable) (Tue Dec 5 17:58:33 2023 +0000) on "macos_arm64"
  • on macos / Version 14.5 (Build 23F79)
  • locale is ru-RU

Process info

Memory CPU Elapsed time Command line
3 MB 0.0% 32-00:43:27 dart devtools --no-launch-browser
11 MB 0.0% 01-21:24:48 dart devtools --no-launch-browser
3 MB 0.0% 37-00:34:47 dart devtools --no-launch-browser
3 MB 0.0% 52-23:03:27 dart devtools --no-launch-browser
4 MB 0.0% 33-11:34:33 dart devtools --no-launch-browser
3 MB 0.0% 53-21:23:57 dart devtools --no-launch-browser
1656 MB 0.0% 01:55:10 dart language-server --client-id=Android-Studio --client-version=AI-233.14808.21 --protocol=analyzer
137 MB 0.0% 01:55:12 flutter_tools.snapshot daemon
90 MB 59.8% 00:01 main.dart-3.4.0.snapshot dart info
@dart-github-bot
Copy link
Collaborator

Summary: The Dart analyzer fails to suggest importing an extension file when a user attempts to use an extension method defined in that file. The expected behavior is for the analyzer to provide a quick-fix that adds the necessary import statement.

@dart-github-bot dart-github-bot added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Jul 28, 2024
@scheglov scheglov added P3 A lower priority bug or feature request analyzer-quick-fix labels Jul 29, 2024
@lrhn lrhn removed the triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. label Jul 31, 2024
@srawlins srawlins changed the title Analyzer doesn't promt quick-fix about extension importing Analyzer doesn't prompt quick fix about extension importing Oct 1, 2024
@srawlins srawlins changed the title Analyzer doesn't prompt quick fix about extension importing Analyzer doesn't prompt quick fix to import for extension in null-aware access Oct 4, 2024
@srawlins
Copy link
Member

srawlins commented Oct 4, 2024

This appears to be related directly to the null-aware component. See this example:

void f(String str1, String? str2){
    str1.map(print);
    str2?.map(print);
}

On both lines I get an error that map is unknown. For the second line, I do not get suggestions to import the library containing map. But for the first line, I do.

@FMorschel
Copy link
Contributor

This is because when inside NotInstantiatedExtensionsExtensions<R>.applicableTo it tests isSubtypeOf testing if String? is a subtype of String which it is not. It doesn't consider the possibility of using ?.. I'll take a look and see if I can fix it later.

https://github.com/dart-lang/sdk/blob/main/pkg/analyzer/lib/src/dart/resolver/applicable_extensions.dart#L245

@srawlins
Copy link
Member

srawlins commented Oct 4, 2024

I have a fix that instead changes SimpleIdentifier.targetType in import_library.dart.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-quick-fix area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P3 A lower priority bug or feature request type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

6 participants