dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.22k stars 1.57k forks source link

Automatically removing `const` for non-constant related errors #53599

Open rasitayaz opened 1 year ago

rasitayaz commented 1 year ago

This tracker is for issues related to:

I apologize beforehand if this is the incorrect place to request this feature.

Dart linter has the ability to automatically remove unnecessary const keywords with unnecessary_const rule, and add them when suggested using prefer_const_constructors if we run dart fix --apply or while saving the file if the editor config is adjusted accordingly.

How about automatically removing the const keyword, when we encounter following errors:

I'm aware that these are not warnings but errors unlike the linter rules I've mentioned above, I'm just wondering if it's possible to make such a fix using the analyzer.

I believe this quick fix would especially be useful in Flutter widget trees.

Use Case Example

class C {
  const C(
    String s, {
    String? s2,
  });
}

void f() {
  const C('constant', s2: 'another constant'); // no errors

  var s = 'non-constant string';

  // const can be removed to fix the errors below
  const C(
    s, // const_with_non_constant_argument & const_constructor_param_type_mismatch errors
    s2: s, // invalid_constant error
  );

  // const can be removed to fix the errors below
  const [
    C('constant'), // no errors
    C(s), // const_with_non_constant_element, const_constructor_param_type_mismatch & non_constant_list_element errors
  ];
}
dart info ``` #### General info - Dart 3.1.1 (stable) (Tue Sep 5 12:20:14 2023 +0000) on "macos_arm64" - on macos / Version 13.5.2 (Build 22G91) - locale is en-TR #### Project info - sdk constraint: '>=3.0.3 <4.0.0' - dependencies: balanced_text, bloc, cached_network_image, conditional_wrap, copy_with_extension, easy_rich_text, email_validator, equatable, expandable_page_view, firebase_core, firebase_crashlytics, flutter, flutter_bloc, flutter_hooks, flutter_keyboard_visibility, flutter_launcher_icons, flutter_localizations, flutter_spinkit, font_awesome_flutter, get_storage, http, intersperse, intl, just_the_tooltip, negative_padding, package_info_plus, pinput, qr_code_scanner, retry, shared_preferences, sliver_tools, sorted, spaced_flex, table_calendar, table_sticky_headers, top_snackbar_flutter, url_launcher, visibility_detector - dev_dependencies: build_runner, copy_with_extension_gen, flutter_lints, flutter_test #### Process info | Memory | CPU | Elapsed time | Command line | | -----: | ---: | -----------: | ------------------------------------------------------------------------------- | | 35 MB | 0.0% | 12:13 | dart devtools --machine --try-ports 10 --allow-embedding | | 8 MB | 0.0% | 08:38:29 | dart devtools --machine --try-ports 10 --allow-embedding | | 52 MB | 0.0% | 12:13 | dart language-server --protocol=lsp --client-id=VS-Code --client-version=3.72.2 | | 17 MB | 0.0% | 08:38:29 | dart language-server --protocol=lsp --client-id=VS-Code --client-version=3.72.2 | | 65 MB | 0.1% | 12:13 | flutter_tools.snapshot daemon | | 29 MB | 0.4% | 08:38:29 | flutter_tools.snapshot daemon | ```
bwilkerson commented 1 year ago

Yes, we have fixes for errors just like we have fixes for warnings and lints, and we could definitely add one here.

I tried a simple test case:

class C {
  C();
}

void f() {
  const [C()]; // errors here
}

Is that the kind of situation you're running into?

I'm seeing two errors: const_with_non_const and non_constant_list_element. If you're seeing both errors in your case, then I think it would make more sense to attach the fix to non_constant_list_element, because then it could be used in more case. If you're only seeing the one, could you provide a short sample of the kind of code you're running into?

rasitayaz commented 1 year ago

After experimenting more thoroughly, I've noticed that this feature could be applied to multiple non-const related scenarios. I've edited the original comment to include some examples.