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

Commit 79fb9c1

Browse files
authored
don't report unnecessary_parenthesis for null-aware cascades (#4043)
1 parent 6f08bd7 commit 79fb9c1

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

lib/src/rules/unnecessary_parenthesis.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class _Visitor extends SimpleAstVisitor<void> {
9696
return;
9797
}
9898
} else if (parent is MethodInvocation) {
99+
if (expression is CascadeExpression) return;
99100
var name = parent.methodName.name;
100101
if (name == 'noSuchMethod' || name == 'toString') {
101102
// Code like `(String).noSuchMethod()` is allowed.

test/rules/all.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ import 'unnecessary_library_directive_test.dart'
8888
as unnecessary_library_directive;
8989
import 'unnecessary_null_checks_test.dart' as unnecessary_null_checks;
9090
import 'unnecessary_overrides_test.dart' as unnecessary_overrides;
91+
import 'unnecessary_parenthesis_test.dart' as unnecessary_parenthesis;
9192
import 'use_build_context_synchronously_test.dart'
9293
as use_build_context_synchronously;
9394
import 'use_enums_test.dart' as use_enums;
@@ -160,6 +161,7 @@ void main() {
160161
unnecessary_library_directive.main();
161162
unnecessary_null_checks.main();
162163
unnecessary_overrides.main();
164+
unnecessary_parenthesis.main();
163165
use_build_context_synchronously.main();
164166
use_enums.main();
165167
use_is_even_rather_than_modulo.main();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:test_reflective_loader/test_reflective_loader.dart';
6+
7+
import '../rule_test_support.dart';
8+
9+
main() {
10+
defineReflectiveSuite(() {
11+
defineReflectiveTests(UnnecessaryParenthesisTest);
12+
});
13+
}
14+
15+
@reflectiveTest
16+
class UnnecessaryParenthesisTest extends LintRuleTest {
17+
@override
18+
String get lintRule => 'unnecessary_parenthesis';
19+
20+
/// https://github.com/dart-lang/linter/issues/4041
21+
test_nullAware_cascadeAssignment() async {
22+
await assertNoDiagnostics(r'''
23+
class A {
24+
var b = false;
25+
void m() {}
26+
}
27+
28+
void f(A? a) {
29+
(a?..b = true)?.m();
30+
}
31+
''');
32+
}
33+
}

0 commit comments

Comments
 (0)