Skip to content

Configuring rules

A rule is configured by its name, under assay: rules: in the project’s root analysis_options.yaml (Configuration). The preset says which rules are on; here you switch one of them off, set its rule parameters or silence it in some paths.

assay:
rules:
no_magic_number: false # a rule switched off
max_function_length: {max: 80} # a rule parameter
avoid_dynamic: {exclude: ["lib/interop/**"]} # silent in some paths

Every rule takes two keys:

Key Effect
enabled Only false has an effect: it switches the rule off. See Switching a rule off and Switching a rule on.
exclude Path globs where this rule alone is silent. Your list replaces the one the preset gives. See Excluding code.

Every other key of a rule is a rule parameter of its own.

assay:
rules:
no_magic_number: false

false is short for {enabled: false}, as for an SDK lint. The rule is then silent in the editor, in dart analyze, in CI and at the gate. It is never hidden: every result names it (config.rulesOff), and so does the console report, in the row Switched off of its head box.

Switching off a rule the preset does not hold does nothing and is no error.

A rule comes on with its preset: the three presets hold more rules each, and the index of rules and checks lists the rules of each.

  • enabled: true cannot switch on a rule your preset does not hold: that is a configuration error that names the preset which holds it. More rules come from a higher preset (Moving from one preset to the next).
  • An SDK lint your preset does not name can be switched on under linter: rules: as always.

A rule is blocking or advisory wherever it is on: no preset or setting changes a rule’s tier. A blocking violation fails the gate; an advisory one never does. The index of rules and checks gives the tier of each rule.

A severity written under analyzer: errors: does not reach assay’s rules either (Differences from the analyzer).

These rules also have rule parameters of their own; their pages say what each does:

Rule Rule parameters (default)
avoid_dynamic allow_json_maps (true)
avoid_non_null_assertion allow_guarded (true)
avoid_late allow_final (false)
no_magic_number allowed ([-1,0,1,2])
no_placeholder_implementation markers (["TODO","FIXME"])
test_requires_assertion test_functions (["test","testWidgets"]), assertion_patterns (["expect*","verify*","assert*","check*","fail"])
test_file_not_collected, duplicate_test_name, empty_test_group test_functions (["test","testWidgets","blocTest"]), group_functions (["group"])
collection_identity_comparison identity_methods (["==","didUpdateWidget","shouldRepaint","shouldRebuild","shouldReclip","shouldRelayout","updateShouldNotify","buildWhen","listenWhen"])
json_cast_to_typed_collection decoders ([])
dispose_owned_resources extra_disposables ({}, a map from a type name to its release method)
text_field_without_value_channel value_arguments ([])
cleartext_request allowed_hosts ([])
secret_in_log, tear_off_in_string, nullable_to_string logging_calls ([])
max_function_length, max_cyclomatic_complexity, max_cognitive_complexity, max_nesting_depth, max_parameters, max_file_length, max_widget_nesting max (the preset’s limit: see Presets)

Every other rule has only enabled and exclude. A value you write replaces the preset’s, or the default, as a whole: a list is never added to.

The limit of a limit rule, max, is on Configuring metrics.

The exclude of a rule lists the path globs where that rule alone is silent. The example, how a glob is read and what your list does to the preset’s are on Excluding code. To silence a rule at one place, use a suppression.