Skip to content

no_magic_string

v1.0.0WarningConfigurableCode Quality

This rule flags the same string literal repeated without a name.

This rule is in the pedantic preset: the threshold is a house style.

Unlike its numeric sibling, this rule reports only repetition — the case where a string is genuinely dangerous. A route path, a storage key or a header name written out at three call sites will eventually be changed at two of them, and the third failure is silent.

A single occurrence is left alone. It is usually a message or a label, and naming it moves the text away from the code that uses it for no gain.

The diagnostic appears at every occurrence rather than only the first: each one is separately editable, and showing one would hide the duplication the rule is about.

Exempt by default: a literal that initialises a declaration, anything inside a const declaration, an enum or an annotation, strings shorter than min_length (separators and punctuation rather than identifiers), and tests, where a repeated fixture string is the test data.

void get_() => send('x-request-id');
void post() => send('x-request-id');
void put() => send('x-request-id');
const requestIdHeader = 'x-request-id';
void get_() => send(requestIdHeader);
void post() => send(requestIdHeader);
void put() => send(requestIdHeader);
analysis_options.yaml
many_lints:
rules:
no_magic_string:
min_occurrences: 3
min_length: 3
additional_ignored_invocations: [translate]
ignore_tests: true
Option Type Default Description
min_occurrences int 3 How many times a string must repeat before it reports
min_length int 3 Shortest string considered; below this it is punctuation
ignored_invocations list of strings [] Constructors and methods whose string arguments never report
additional_ignored_invocations list of strings [] Names to add without replacing ignored_invocations
ignore_tests bool true Skip files under test/

To disable this rule:

many_lints.yaml
rules:
no_magic_string: false

To keep the rule on but skip certain paths, use per-rule exclude.