Skip to content

Commit a186457

Browse files
srawlinsCommit Queue
authored andcommitted
lint: correct some simple unnecessary_parenthesis cases found internally
Fixes https://github.com/dart-lang/linter/issues/4354 Cq-Include-Trybots: luci.dart.try:flutter-analyze-try,analyzer-win-release-try,pkg-win-release-try Change-Id: Ie0138093467ff266d4f06c70f280660c4fc0783d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/376540 Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Phil Quitslund <[email protected]>
1 parent a7158eb commit a186457

File tree

6 files changed

+149
-96
lines changed

6 files changed

+149
-96
lines changed

pkg/analyzer/lib/src/lint/pub.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class PSEntry {
143143
PSEntry(this.key, this.value);
144144

145145
@override
146-
String toString() => '${key != null ? ('$key: ') : ''}$value';
146+
String toString() => '${key != null ? '$key: ' : ''}$value';
147147
}
148148

149149
/// Representation of environment in `pubspec.yaml`.

pkg/linter/lib/src/extensions.dart

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,29 +100,29 @@ extension AstNodeNullableExtension on AstNode? {
100100
return null;
101101
}
102102

103-
bool get isFieldNameShortcut {
104-
var node = this;
105-
if (node is NullCheckPattern) node = node.parent;
106-
if (node is NullAssertPattern) node = node.parent;
107-
return node is PatternField && node.name != null && node.name?.name == null;
108-
}
109-
110-
/// Return `true` if the expression is null aware, or if one of its recursive
111-
/// targets is null aware.
112-
bool containsNullAwareInvocationInChain() {
103+
/// Whether the expression is null-aware, or if one of its recursive targets
104+
/// is null-aware.
105+
bool get containsNullAwareInvocationInChain {
113106
var node = this;
114107
if (node is PropertyAccess) {
115108
if (node.isNullAware) return true;
116-
return node.target.containsNullAwareInvocationInChain();
109+
return node.target.containsNullAwareInvocationInChain;
117110
} else if (node is MethodInvocation) {
118111
if (node.isNullAware) return true;
119-
return node.target.containsNullAwareInvocationInChain();
112+
return node.target.containsNullAwareInvocationInChain;
120113
} else if (node is IndexExpression) {
121114
if (node.isNullAware) return true;
122-
return node.target.containsNullAwareInvocationInChain();
115+
return node.target.containsNullAwareInvocationInChain;
123116
}
124117
return false;
125118
}
119+
120+
bool get isFieldNameShortcut {
121+
var node = this;
122+
if (node is NullCheckPattern) node = node.parent;
123+
if (node is NullAssertPattern) node = node.parent;
124+
return node is PatternField && node.name != null && node.name?.name == null;
125+
}
126126
}
127127

128128
extension BlockExtension on Block {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class _Visitor extends SimpleAstVisitor<void> {
6767
node.argumentList.arguments.isNotEmpty &&
6868
node.argumentList.arguments.first is FunctionExpression &&
6969
_isIterable(target.staticType) &&
70-
!node.containsNullAwareInvocationInChain() &&
70+
!node.containsNullAwareInvocationInChain &&
7171
!_hasMethodChaining(node) &&
7272
!_isInsideCascade(node)) {
7373
rule.reportLint(node.function);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class _Visitor extends SimpleAstVisitor<void> {
204204
}
205205

206206
var checker = _FinalExpressionChecker(parameters);
207-
if (!node.containsNullAwareInvocationInChain() &&
207+
if (!node.containsNullAwareInvocationInChain &&
208208
checker.isFinalNode(node.target) &&
209209
node.methodName.staticElement.isFinal &&
210210
node.typeArguments == null) {

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ class _Visitor extends SimpleAstVisitor<void> {
104104
// `(List<int>).toString()` is OK.
105105
if (expression is TypeLiteral) return;
106106

107-
if (expression is SimpleIdentifier ||
108-
expression.containsNullAwareInvocationInChain()) {
107+
if (expression.isOneToken ||
108+
expression.containsNullAwareInvocationInChain) {
109109
if (parent is PropertyAccess) {
110110
var name = parent.propertyName.name;
111111
if (name == 'hashCode' || name == 'runtimeType') {
@@ -233,9 +233,10 @@ class _Visitor extends SimpleAstVisitor<void> {
233233
// inside one of the following nodes, the readability is not affected.
234234
if (parent is! AssignmentExpression &&
235235
parent is! ConstructorFieldInitializer &&
236-
parent is! VariableDeclaration &&
237236
parent is! ExpressionFunctionBody &&
237+
parent is! RecordLiteral &&
238238
parent is! ReturnStatement &&
239+
parent is! VariableDeclaration &&
239240
parent is! YieldStatement &&
240241
!node.isArgument) {
241242
return;
@@ -350,4 +351,15 @@ extension on Expression {
350351
bool get isArgument =>
351352
parent is ArgumentList ||
352353
(parent is NamedExpression && parent?.parent is ArgumentList);
354+
355+
/// Whether this expression is a sigle token.
356+
///
357+
/// This excludes type literals because they often need to be parenthesized.
358+
bool get isOneToken =>
359+
this is SimpleIdentifier ||
360+
this is StringLiteral ||
361+
this is IntegerLiteral ||
362+
this is DoubleLiteral ||
363+
this is NullLiteral ||
364+
this is BooleanLiteral;
353365
}

0 commit comments

Comments
 (0)