valid-regexps
Same as valid_regexps in Dart lints
- Group
- Correctness
- Path
lint/correctness/valid-regexps- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 510 of 512
Disallow statically known invalid dart:core regular expressions.
Validate decoded literal patterns with an ECMAScript parser, including the
constructor's literal unicode: true flag, matching the SDK lint.
Invalid
// Bad: literal patterns that are structurally invalid regular expressions.
void examples() {
final a = RegExp('(');
final b = RegExp(')');
final c = RegExp('[a-z');
final d = RegExp('(abc');
final e = RegExp('a(b(c)');
final f = new RegExp('foo)');
final g = RegExp('*abc');
final h = RegExp('[z-a]');
final i = RegExp(r'\xG0', unicode: true);
final j = RegExp('a{3,2}');
final k = RegExp(r'[z-a]');
final l = RegExp(r'\d{3,2}');
final adjacent = RegExp('(' r'\d');
print([a, b, c, d, e, f, g, h, i, j, k, l]);
RegExp('{', unicode: true);
RegExp(r'\q', unicode: true);
RegExp(r'(?invalid)');
RegExp(r'(?<x>a)(?<x>b)');
RegExp(r'(?<=a)+');
RegExp('\\xG0', unicode: true);
RegExp('\\');
}void examples() { final a = RegExp('('); final a = RegExp('('); final b = RegExp(')'); final b = RegExp(')'); final c = RegExp('[a-z'); final c = RegExp('[a-z'); final d = RegExp('(abc'); final d = RegExp('(abc'); final e = RegExp('a(b(c)'); final e = RegExp('a(b(c)'); final f = new RegExp('foo)'); final f = new RegExp('foo)'); final g = RegExp('*abc'); final g = RegExp('*abc'); final h = RegExp('[z-a]'); final h = RegExp('[z-a]'); final i = RegExp(r'\xG0', unicode: true); final i = RegExp(r'\xG0', unicode: true); final j = RegExp('a{3,2}'); final j = RegExp('a{3,2}'); final k = RegExp(r'[z-a]'); final k = RegExp(r'[z-a]'); final l = RegExp(r'\d{3,2}'); final l = RegExp(r'\d{3,2}'); final adjacent = RegExp('(' r'\d'); print([a, b, c, d, e, f, g, h, i, j, k, l]); RegExp('{', unicode: true); RegExp('{', unicode: true); RegExp(r'\q', unicode: true); RegExp(r'\q', unicode: true); RegExp(r'(?invalid)'); RegExp(r'(?invalid)'); RegExp(r'(?<x>a)(?<x>b)'); RegExp(r'(?<x>a)(?<x>b)'); RegExp(r'(?<=a)+'); RegExp(r'(?<=a)+'); RegExp('\\xG0', unicode: true); RegExp('\\xG0', unicode: true); RegExp('\\');Valid
// Good: valid patterns, or patterns that are not statically checkable literals.
void examples(String dynamicPattern) {
final a = RegExp('abc');
final b = RegExp('[a-z]+');
final c = RegExp('(foo|bar)');
final d = RegExp('a(b(c)d)e'); // balanced nested groups
final e = RegExp(r'\d{3}-\d{4}'); // raw string, balanced
final f = RegExp('\\d+'); // decoded non-raw pattern
final g = RegExp(dynamicPattern); // not a literal, skipped
final h = RegExp('prefix-${dynamicPattern}'); // interpolated, skipped
final i = RegExp(r'(?=foo)foo'); // lookahead
final j = RegExp(r'(?:foo)*?'); // non-capturing group and lazy quantifier
final k = RegExp('[]'); // ECMAScript empty character class
final l = RegExp('[a-]'); // trailing hyphen is a literal, not a range
final m = RegExp(r'[a-' r'z]');
final n = RegExp(r'\d{1,' r'3}');
final o = RegExp('[a-' 'z]');
final p = RegExp(r'''[a-z]+''');
final adjacent = RegExp('(' r'\d' ')');
print([a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p]);
RegExp('{');
RegExp('a{not-a-quantifier}');
RegExp(r'\xG0');
RegExp(r'\uZZZZ');
RegExp(r'\q');
RegExp(r'(?<=a)b');
RegExp(r'(?<!a)b');
RegExp(r'(?<word>a)\k<word>');
RegExp(r'(?=a)+');
RegExp(r'\p{Letter}+', unicode: true);
RegExp(r'\u{1F600}', unicode: true);
}Configure valid-regexps
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"correctness": {
"valid-regexps": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/valid-regexps: <reason>More correctness rules
- void-checks
void_checksrecommendedDon't pass or assign values to
void(orFutureOr<void>) locations. - add-equatable-propsrecommendedequatable
Flags concrete Equatable subclasses that do not declare an instance props property.
- always-pass-global-keyflutter
Keep
GlobalKeysstable and pass owned State fields to widgets. - always-remove-listenerrecommendedflutter
Requires Flutter Listenable registrations in State to be removed.
- always-use-package-imports
always_use_package_importsDisallow relative imports in libraries under a package's
lib/directory. - avoid-assigning-to-static-field
Flags instance methods writing resolved static fields, including compound assignments and increments.
- avoid-calling-notifier-members-inside-buildrecommendedriverpod
Flags calls to Riverpod notifier methods during widget build.
- avoid-collection-equality-checksrecommended
Flags equality comparisons between non-constant SDK collections.