Skip to content

Lints (e.g., prefer_const_constructors) not applied via include: in analysis_options.yaml with Dart SDK 3.7.2 / Flutter 3.29.3 #60652

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

Closed
JaseElder opened this issue Apr 30, 2025 · 6 comments
Labels
area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. devexp-linter Issues with the analyzer's support for the linter package P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@JaseElder
Copy link

Const warnings are not being triggered by the analyzer unless the rules are explicitly added to analysis_options.yaml

The rules:

linter:
  rules:
    prefer_const_constructors: true
    prefer_const_declarations: true
    prefer_const_constructors_in_immutables: true
    prefer_const_literals_to_create_immutables: true
    unnecessary_const: true

If these rules aren't present, then no warnings about missing const keywords appear in the IDE.
This is happening on a fresh flutter project and existing projects on the same machine.

Using flutter_lints: ^5.0.0
The same behaviour was experienced with lints: ^5.1.1 using include: package:lints/recommended.yaml

The behaviour has been confirmed running flutter analyze and using the Dart Analysis Server through Android Studio

Output of flutter doctor:

doctor --verbose
[✓] Flutter (Channel stable, 3.29.3, on macOS 15.4.1 24E263 darwin-x64, locale en-GB) [2.2s]
    • Flutter version 3.29.3 on channel stable at /Users/jasonelder/Documents/Flutter/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ea121f8859 (3 weeks ago), 2025-04-11 19:10:07 +0000
    • Engine revision cf56914b32
    • Dart version 3.7.2
    • DevTools version 2.42.3

[✓] Android toolchain - develop for Android devices (Android SDK version 36.0.0) [6.5s]
    • Android SDK at /Users/jasonelder/Library/Android/sdk
    • Platform android-36, build-tools 36.0.0
    • ANDROID_HOME = /Users/jasonelder/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
      This is the JDK bundled with the latest Android Studio installation on this machine.
      To manually set the JDK path, use: `flutter config --jdk-dir="path/to/jdk"`.
    • Java version OpenJDK Runtime Environment (build 21.0.6+-13355223-b631.42)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 16.3) [7.0s]
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 16E140
    • CocoaPods version 1.16.2

[✓] Chrome - develop for the web [190ms]
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2024.3) [188ms]
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 21.0.6+-13355223-b631.42)

[✓] IntelliJ IDEA Community Edition (version 2024.3.1.1) [183ms]
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] Connected device (3 available) [8.4s]
    • SM T395 (mobile) • 5200cc794383b581 • android-arm    • Android 9 (API 28)
    • macOS (desktop)  • macos            • darwin-x64     • macOS 15.4.1 24E263 darwin-x64
    • Chrome (web)     • chrome           • web-javascript • Google Chrome 135.0.7049.115

[✓] Network resources [860ms]
    • All expected network resources are available.

• No issues found!

Contents of analysis_options.yaml:


# This file configures the analyzer, which statically analyzes Dart code to
# check for errors, warnings, and lints.
#
# The issues identified by the analyzer are surfaced in the UI of Dart-enabled
# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be
# invoked from the command line by running `flutter analyze`.

# The following line activates a set of recommended lints for Flutter apps,
# packages, and plugins designed to encourage good coding practices.
include: package:flutter_lints/flutter.yaml

linter:
  # The lint rules applied to this project can be customized in the
  # section below to disable rules from the `package:flutter_lints/flutter.yaml`
  # included above or to enable additional rules. A list of all available lints
  # and their documentation is published at https://dart.dev/lints.
  #
  # Instead of disabling a lint rule for the entire project in the
  # section below, it can also be suppressed for a single line of code
  # or a specific dart file by using the `// ignore: name_of_lint` and
  # `// ignore_for_file: name_of_lint` syntax on the line or in the file
  # producing the lint.
  rules:
    # avoid_print: false  # Uncomment to disable the `avoid_print` rule
    # prefer_single_quotes: true  # Uncomment to enable the `prefer_single_quotes` rule
    prefer_const_constructors: true
    prefer_const_declarations: true
    prefer_const_constructors_in_immutables: true
    prefer_const_literals_to_create_immutables: true
    unnecessary_const: true
# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options

Let me know if you need any more info.

@JaseElder JaseElder added the area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. label Apr 30, 2025
@bwilkerson
Copy link
Member

I'm not sure I'm understanding the problem. Are you saying that, given the analysis_options.yaml file above, the prefer_const_constructors_in_immutables will report diagnostics, but that if you remove the line containing prefer_const_constructors_in_immutables: true from the options file that the diagnostics produced by that rule before you remove that line are no longer reported?

If so, do you have a dependency on flutter_lints in the pubspec.yaml file? Have run flutter pub get?

@JaseElder
Copy link
Author

JaseElder commented Apr 30, 2025

I'm saying that, without those explicit rules in the analysis_options.yaml file, the analyser doesn't highlight instances where (for instance) the constmodifier isn't used in a constructor invocation, like:

  @override
  Widget build(BuildContext context) {
    return Scaffold(body: SplashScreen());
  }

where SplashScreen() has a const constructor, so the correct code would be:

  @override
  Widget build(BuildContext context) {
    return const Scaffold(body: SplashScreen());
  }

In previous versions of Flutter/Dart, those instances would be highlighted without the rules having to be explicitly specified.
I have a dependency on flutter_lints: flutter_lints: ^5.0.0 and flutter pub get has been run.
This behaviour is happening in both newly created flutter projects and old established ones, where the correct behaviour was occurring at some stage in the past - I'm not sure on which version of Dart/Flutter it stopped.

ETA: I'm not sure if the specific rule prefer_const_constructors_in_immutables was an issue, as I don't have an instance of the @immutable annotation in my current code. I just added all the const rules I could find.

@bwilkerson
Copy link
Member

Thanks for the clarification. That's definitely a bug. Those rules should be enabled as a result of the include.

@bwilkerson bwilkerson added devexp-linter Issues with the analyzer's support for the linter package P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Apr 30, 2025
@JaseElder
Copy link
Author

Ah, actually it was done on purpose:
flutter/packages@54dd888

Do you know why that would have happened?

@bwilkerson
Copy link
Member

I believe the decision was a result of the discussion in dart-lang/core#833.

I'll close this as WAI.

@JaseElder
Copy link
Author

Thanks for the extra info!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. devexp-linter Issues with the analyzer's support for the linter package P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

2 participants