no-magic-string
- Group
- Suspicious
- Path
lint/suspicious/no-magic-string- Default
- Off, opt in
- Origin
- Falcon
- Rule
- 293 of 512
Extract inline string literals into named variables or constants.
Exempts direct literal initializers, defaults, annotations, constant collections,
indices, asserts, and direct string arguments of regex/URI/error constructors,
common string operations and ignored-invocations (default print, debugPrint).
allow-only-once (false) allows values occurring only once across expression/pattern literals.
Literal patterns are exempt from diagnostics but still count; directive strings do not count.
Invalid
void f(dynamic value) {
consume('message');
final instance = Model('inline');
if (value == 'ready') {}
returnValue('result');
value.message = 'updated';
consume(r'raw');
if ('admin'.contains(value)) {}
'a-b'.split('-');
value.contains(User(name: 'admin'));
print(User(name: 'nested'));
final s = '${lookup("magic-key")}';
}void f(dynamic value) { consume('message'); consume('message'); final instance = Model('inline'); final instance = Model('inline'); if (value == 'ready') {} if (value == 'ready') {} returnValue('result'); returnValue('result'); value.message = 'updated'; value.message = 'updated'; consume(r'raw'); consume(r'raw'); if ('admin'.contains(value)) {} if ('admin'.contains(value)) {} 'a-b'.split('-'); 'a-b'.split('-'); value.contains(User(name: 'admin')); value.contains(User(name: 'admin')); print(User(name: 'nested')); print(User(name: 'nested')); final s = '${lookup("magic-key")}';Valid
import 'dart:core';
export 'dart:core';
const title = 'Title';
class Model { final label = 'Label'; }
void f(dynamic value, [String fallback = 'Fallback']) {
final message = 'message';
print('log');
debugPrint('debug');
assert(value != null, 'required');
RegExp('pattern');
Uri.https('example.com');
StateError('error');
ArgumentError.value(value, 'count', 'must be positive');
RangeError.range(value, 0, 9, 'index');
value.split(',');
const map = {'key': 'value'};
value['key'];
consume('');
value..['cascade-key'] = 1;
value..['cascade-read'].toString();
consume({'serialized-key': value});
value.contains('direct');
value.contains(('parenthesized'));
print(message: 'named');
final parenthesized = ('initializer');
final interpolated = '${value}';
}
enum Env {
dev('https://dev.example.com');
const Env(this.url);
final String url;
}
void patterns(dynamic value) {
if (value case 'ready') {}
switch (value) {
case 'a':
break;
default:
break;
}
final matched = switch (value) { 'b' => true, _ => false };
if (value case {'user': var name}) {}
}
class Marker { const Marker(String label); }
@Marker('annotation')
void annotated() {}Configure no-magic-string
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"suspicious": {
"no-magic-string": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/suspicious/no-magic-string: <reason>More suspicious rules
- no-self-comparisons
no_self_comparisonsrecommendedFlags proven self-comparisons of side-effect-free scalar values.
- no-wildcard-variable-uses
no_wildcard_variable_usesrecommendedFlags references to a wildcard variable or parameter (a name made solely of underscores, such as
_or__). - overridden-fields
overridden_fieldsrecommendedFlags instance fields that redeclare an inherited field.
- prefer-unique-test-namestest
Reports duplicate test descriptions within a group and constant names in loops.
- recursive-getters
recursive_gettersrecommendedFlags a getter that unconditionally returns itself.
- unnecessary-null-aware-assignments
unnecessary_null_aware_assignmentsrecommendedFlags
x ??= null, which can never have an effect. - unnecessary-null-in-if-null-operators
unnecessary_null_in_if_null_operatorsrecommendedFlags
x ?? nullandnull ?? x, where thenulloperand is redundant. - use-rethrow-when-possible
use_rethrow_when_possiblerecommendedFlags
throw einside acatch (e)by matching the exception parameter's name.