Skip to content

Commit 08cda24

Browse files
RohitSailyCommit Queue
authored andcommitted
Fix literal_only_boolean_expressions is False-Positive
Prior to this commit - a conditonal expression which used `is` to test against a type parameter was treated as literal expression causing the lint to flag such conditions. The actual type of a type parameter is not known for a program until a literal type is substituted hence the flagging was a false-positive. This commit prevents such flagging. Change-Id: I7b33510e83354fb652b159df452ed5063f51e875 Bug: #61903 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/461680 Commit-Queue: Samuel Rawlins <[email protected]> Auto-Submit: Rohit Saily <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]>
1 parent b262f60 commit 08cda24

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

pkg/linter/lib/src/rules/literal_only_boolean_expressions.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:analyzer/analysis_rule/rule_visitor_registry.dart';
88
import 'package:analyzer/dart/ast/ast.dart';
99
import 'package:analyzer/dart/ast/token.dart';
1010
import 'package:analyzer/dart/ast/visitor.dart';
11+
import 'package:analyzer/dart/element/element.dart';
1112
import 'package:analyzer/error/error.dart';
1213

1314
import '../analyzer.dart';
@@ -31,6 +32,10 @@ bool _onlyLiterals(Expression? rawExpression) {
3132
return _onlyLiterals(expression.leftOperand) &&
3233
_onlyLiterals(expression.rightOperand);
3334
}
35+
if (expression is IsExpression) {
36+
if (expression.type.type?.element is TypeParameterElement) return false;
37+
return _onlyLiterals(expression.expression);
38+
}
3439
return false;
3540
}
3641

pkg/linter/test/rules/literal_only_boolean_expressions_test.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,33 @@ void f() {
218218
''');
219219
}
220220

221+
test_is_expression_both_operands_known() async {
222+
await assertDiagnostics(
223+
r'''
224+
void f() {
225+
if (20 is int) {}
226+
}
227+
''',
228+
[lint(13, 17), error(diag.unnecessaryTypeCheckTrue, 17, 9)],
229+
);
230+
}
231+
232+
test_is_expression_only_left_operand_unknown() async {
233+
await assertNoDiagnostics(r'''
234+
void f(a) {
235+
if (a is int) {}
236+
}
237+
''');
238+
}
239+
240+
test_is_expression_only_right_operand_unknown() async {
241+
await assertNoDiagnostics(r'''
242+
void f<T>(a) {
243+
if (20 is T) {}
244+
}
245+
''');
246+
}
247+
221248
test_nullAware() async {
222249
await assertDiagnostics(
223250
r'''

0 commit comments

Comments
 (0)