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

Commit

Permalink
don't report unnecessary_parenthesis for null-aware cascades (#4043)
Browse files Browse the repository at this point in the history
  • Loading branch information
pq authored Feb 2, 2023
1 parent 6f08bd7 commit 79fb9c1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/src/rules/unnecessary_parenthesis.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class _Visitor extends SimpleAstVisitor<void> {
return;
}
} else if (parent is MethodInvocation) {
if (expression is CascadeExpression) return;
var name = parent.methodName.name;
if (name == 'noSuchMethod' || name == 'toString') {
// Code like `(String).noSuchMethod()` is allowed.
Expand Down
2 changes: 2 additions & 0 deletions test/rules/all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ import 'unnecessary_library_directive_test.dart'
as unnecessary_library_directive;
import 'unnecessary_null_checks_test.dart' as unnecessary_null_checks;
import 'unnecessary_overrides_test.dart' as unnecessary_overrides;
import 'unnecessary_parenthesis_test.dart' as unnecessary_parenthesis;
import 'use_build_context_synchronously_test.dart'
as use_build_context_synchronously;
import 'use_enums_test.dart' as use_enums;
Expand Down Expand Up @@ -160,6 +161,7 @@ void main() {
unnecessary_library_directive.main();
unnecessary_null_checks.main();
unnecessary_overrides.main();
unnecessary_parenthesis.main();
use_build_context_synchronously.main();
use_enums.main();
use_is_even_rather_than_modulo.main();
Expand Down
33 changes: 33 additions & 0 deletions test/rules/unnecessary_parenthesis_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:test_reflective_loader/test_reflective_loader.dart';

import '../rule_test_support.dart';

main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnnecessaryParenthesisTest);
});
}

@reflectiveTest
class UnnecessaryParenthesisTest extends LintRuleTest {
@override
String get lintRule => 'unnecessary_parenthesis';

/// https://github.com/dart-lang/linter/issues/4041
test_nullAware_cascadeAssignment() async {
await assertNoDiagnostics(r'''
class A {
var b = false;
void m() {}
}
void f(A? a) {
(a?..b = true)?.m();
}
''');
}
}

0 comments on commit 79fb9c1

Please sign in to comment.