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

unnecessary_parenthesis: parenthesized pattern precedence (WIP) #4075

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/src/rules/unnecessary_parenthesis.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class UnnecessaryParenthesis extends LintRule {
NodeLintRegistry registry, LinterContext context) {
var visitor = _Visitor(this);
registry.addParenthesizedExpression(this, visitor);
registry.addParenthesizedPattern(this, visitor);
}
}

Expand All @@ -84,6 +85,15 @@ class _Visitor extends SimpleAstVisitor<void> {

_Visitor(this.rule);

@override
void visitParenthesizedPattern(ParenthesizedPattern node) {
//https://github.com/dart-lang/linter/issues/4062
var parent = node.parent;
if (parent is DartPattern && parent.precedence < node.pattern.precedence) {
rule.reportLint(node);
}
}

@override
void visitParenthesizedExpression(ParenthesizedExpression node) {
var parent = node.parent;
Expand Down
25 changes: 25 additions & 0 deletions test/rules/unnecessary_parenthesis_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,31 @@ void f(int i) {
}
''');
}

///https://github.com/dart-lang/linter/issues/4062
//@FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/51426')
@soloTest
test_parenthesized_listPattern() async {
await assertDiagnostics(r'''
void f(List<int> l) {
if (l case [(<3), 12]) return;
}
''', [
lint(41, 11),
]);
}

@soloTest
test_parenthesized_relationalPattern() async {
await assertDiagnostics(r'''
void f(List<int> l) {
if (l case [(<3) || (>5)]) return;
}
''', [
lint(36, 4),
lint(44, 4),
]);
}
}

@reflectiveTest
Expand Down