Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
fix literal_only_boolean_expressions if-case false positives (#4355)
Browse files Browse the repository at this point in the history
  • Loading branch information
pq authored May 15, 2023
1 parent 77bcf8c commit 7f3b3a4
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/src/rules/literal_only_boolean_expressions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ class _Visitor extends SimpleAstVisitor<void> {

@override
void visitIfStatement(IfStatement node) {
if (node.caseClause != null) return;
if (_onlyLiterals(node.expression)) {
rule.reportLint(node);
}
Expand Down
71 changes: 71 additions & 0 deletions test/rules/literal_only_boolean_expressions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,77 @@ class LiteralOnlyBooleanExpressionsTestLanguage300 extends LintRuleTest
@override
String get lintRule => 'literal_only_boolean_expressions';

test_ifCase_intLiteral() async {
await assertDiagnostics(r'''
void f() {
if (1 case {1:0}) {
print('');
}
}
''', [
// No lint
error(WarningCode.PATTERN_NEVER_MATCHES_VALUE_TYPE, 24, 5),
]);
}

test_ifCase_listLiteral() async {
await assertNoDiagnostics(r'''
void f() {
if ([1] case [1]) {
print('');
}
}
''');
}

test_ifCase_mapLiteral() async {
await assertNoDiagnostics(r'''
void f() {
if ({1:0} case {1:0}) {
print('');
}
}
''');
}

test_ifCase_objectInstantiation() async {
await assertNoDiagnostics(r'''
class A {
final int a;
A(this.a);
}
void f() {
if (A(1) case A(a: 1)) {
print('');
}
}
''');
}

/// https://github.com/dart-lang/linter/issues/4352
test_ifCase_record() async {
await assertNoDiagnostics(r'''
void f() {
if (('', '') case (String(), String())) {
print('');
}
}
''');
}

test_switchExpression() async {
await assertNoDiagnostics(r'''
bool f(Object o) => switch(o) {
[1] => true,
{1:1} => false,
(1, 1) => false,
String(isEmpty: false) => false,
_ => false,
};
''');
}

test_whenClause() async {
await assertDiagnostics(r'''
void f() {
Expand Down

0 comments on commit 7f3b3a4

Please sign in to comment.