Flutter Dart analyzer not applying my linter rules

Issue

I’m working on a pretty standard Flutter project. Because I’m new to Flutter and Dart, I’d like my tools to be as helpful as possible. So I added pedantic: ^1.9.0 to dev_dependencies and wrote analysis_options.yaml like this:

include: package:pedantic/analysis_options.yaml

analyzer:
  exclude: [build/**]
  strong-mode:
    implicit-casts: false
    implicit-dynamic: false

According to https://dart-lang.github.io/linter/lints/, pedantic should enable the avoid_empty_else and avoid_relative_lib_imports lints, among others, as errors. But when I write code like:

import '../model/model.dart';

or this:

  if (context == null) {
    print('context is null');
  } else {
  }

I’m not getting any errors in IntelliJ IDEA, nor when I run flutter analyze manually:

$ flutter analyze
Analyzing app...                                                        
No issues found! (ran in 5.0s)

I’ve tried enabling these lints explicitly:

linter:
  rules:
    - avoid_empty_else
    - avoid_relative_lib_imports

This doesn’t make any difference.

I’ve tried adding a nonexistent lint foo to that list to verify that the file is being used, and it is:

$ flutter analyze
Analyzing app...                                                        

warning • 'foo' is not a recognized lint rule • analysis_options.yaml:12:7 • undefined_lint_warning

1 issue found. (ran in 4.9s)

I even tried running the dartanalyzer from the Flutter installation directory directly, with all verbosity options that I could find:

$ ~/flutter/bin/cache/dart-sdk/bin/dartanalyzer --lints --verbose --log --options analysis_options.yaml .
Analyzing app...
Loaded analysis options from analysis_options.yaml
Analysis options: lints = true
No issues found!

For completeness, here’s my doctor output:

$ flutter doctor -v
[✓] Flutter (Channel stable, v1.17.1, on Linux, locale en_US.UTF-8)
    • Flutter version 1.17.1 at /home/thomas/flutter
    • Framework revision f7a6a7906b (5 days ago), 2020-05-12 18:39:00 -0700
    • Engine revision 6bc433c6b6
    • Dart version 2.8.2


[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at /opt/android-sdk
    • Platform android-28, build-tools 28.0.3
    • ANDROID_HOME = /opt/android-sdk
    • ANDROID_SDK_ROOT = /opt/android-sdk
    • Java binary at: /usr/lib/jvm/default/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-b08)
    • All Android licenses accepted.

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/docs/get-started/install/linux#android-setup for detailed instructions).

[✓] IntelliJ IDEA Community Edition (version 2019.3)
    • IntelliJ at /usr/share/jetbrains-idea-ce
    • Flutter plugin version 44.0.3
    • Dart plugin version 193.6911.31

[✓] Connected device (1 available)
    • FP2 • 1e95f6f3 • android-arm • Android 7.1.2 (API 25)

! Doctor found issues in 1 category.

Is there something else I should do to get the linter to work?

Solution

Ah, so… the linter is working fine; it was just my assumptions that were broken.

avoid_empty_else does not check for empty {} blocks, but only for ; right after else, which is why it didn’t trigger.

avoid_relative_lib_imports literally only checks for relative imports whose path contains /lib/ in the name, not for relative imports whose target resolves to some file inside lib/.

Bummer. I was hoping to forbid relative imports altogether, but that’s still unimplemented.

Answered By – Thomas

Answer Checked By – David Marino (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.