PreferFirstLastRule class
Rule that prefers .first/.last over index access.
Using .first and .last is more expressive and idiomatic Dart
compared to accessing 0 or length - 1.
Known Limitations
This rule operates on syntactic patterns without resolved type information. As a result, it may produce false positives in the following cases:
-
String indexing:
Stringsupports[0]but does not have.firstor.lastproperties. The rule cannot distinguishStringfromList.String s = "hello"; print(s[0]); // Will trigger, but s.first doesn't exist -
Custom indexable types: Any type with
operator[]but without.first/.lastgetters will trigger false positives.
Recommended Mitigation
Exclude files with heavy string manipulation:
anteater:
rules:
- prefer-first-last:
exclude:
- '**/string_utils.dart'
- '**/text_*.dart'
Constructors
Properties
- category → RuleCategory
-
Category this rule belongs to.
no setteroverride
- defaultSeverity → RuleSeverity
-
Default severity for violations of this rule.
no setteroverride
- description → String
-
Human-readable description of what this rule checks.
no setteroverride
- documentationUrl → String?
-
Documentation URL for this rule.
no setterinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
- id → String
-
Unique identifier for this rule (e.g., 'avoid-dynamic').
no setteroverride
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
check(
CompilationUnit unit, {LineInfo? lineInfo}) → List< Violation> -
Check the compilation unit for violations.
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited