-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
Description
The cascade operator (..) is a powerful feature in Dart, but its misuse with methods that return new instances (instead of modifying the original object) can lead to subtle and hard-to-detect bugs. Currently, there is no lint to warn developers about such scenarios, which could improve code reliability and readability.
Problem Example
The following code uses the cascade operator in a way that appears valid but leads to unintended behavior:
final uri = Uri.parse('https://api.example.com/products')..replace(
queryParameters: {
'category': 'electronics',
'sort': 'price_asc',
}
);
print(uri); // Incorrect: Prints https://api.example.com/products
print(uri.queryParameters); // Incorrect: Prints {}In this example:
Uri.replacecreates a newUriinstance but does not modify the original object.- The cascade operator (
..) applies thereplacemethod, but the result is discarded, leading to incorrect behavior.
Correct Example
To achieve the intended behavior, the method’s result must be explicitly assigned to the variable:
final uri = Uri.parse('https://api.example.com/products').replace(
queryParameters: {
'category': 'electronics',
'sort': 'price_asc',
}
);
print(uri); // Correct: Prints https://api.example.com/products?category=electronics&sort=price_asc
print(uri.queryParameters); // Correct: Prints {category: electronics, sort: price_asc}Proposed Solution
Introduce a new lint that warns when:
- The cascade operator (
..) is used with methods that return a new instance (instead of modifying the original object). - The return value of such methods is not used, leading to potentially unintended behavior.
Expected Behavior
For the following code:
final uri = Uri.parse('https://api.example.com/products')..replace(
queryParameters: {'category': 'electronics', 'sort': 'price_asc'}
);The lint should produce a warning, such as:
"Avoid using the cascade operator with methods that return new instances. The result of
replaceis ignored."
Rationale
-
Developer Experience:
- This lint would help developers catch unintended usage of the cascade operator during development.
- It aligns with Dart's philosophy of developer-friendly tools and clarity.
-
Common Pitfalls:
- Misuse of cascade operators with immutable objects is a common issue, especially for newcomers to Dart.
Challenges
-
Complexity:
- Detecting all methods that return new instances (versus those with side effects) might require additional metadata or analysis.
- The lint should not flag valid uses of the cascade operator with side-effecting methods.
-
Performance:
- Ensuring this lint does not introduce significant performance overhead during static analysis.
Additional Notes
This lint could be particularly useful when working with:
- Immutable classes (e.g.,
Uri,String,DateTime, etc.). - APIs that follow a builder or fluent pattern, where methods return modified instances instead of modifying the original.
Request
Please consider adding this lint to help improve code quality and reduce subtle bugs caused by improper use of the cascade operator. If this suggestion is deemed infeasible, a documentation update clarifying this behavior would also be valuable.