Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 77da201

Browse files
bwilkersoncommit-bot@chromium.org
authored andcommitted
Add a fix for unnecessary_nullable_for_final_variable_declarations
Change-Id: Ibd6570c05f3a289fba72c42f3fc30e88649a9004 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/185000 Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 4216dc8 commit 77da201

File tree

7 files changed

+144
-11
lines changed

7 files changed

+144
-11
lines changed

pkg/analysis_server/lib/src/services/correction/dart/remove_question_mark.dart

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,24 @@ class RemoveQuestionMark extends CorrectionProducer {
1515

1616
@override
1717
Future<void> compute(ChangeBuilder builder) async {
18-
if (node is! TypeName) {
19-
return;
18+
var node = this.node;
19+
if (node is VariableDeclaration) {
20+
var parent = node.parent;
21+
if (parent is VariableDeclarationList) {
22+
node = parent.type;
23+
} else {
24+
return;
25+
}
2026
}
21-
var typeName = node as TypeName;
22-
var questionMark = typeName.question;
23-
if (questionMark == null) {
24-
return;
27+
if (node is TypeName) {
28+
var questionMark = node.question;
29+
if (questionMark == null) {
30+
return;
31+
}
32+
await builder.addDartFileEdit(file, (builder) {
33+
builder.addDeletion(range.token(questionMark));
34+
});
2535
}
26-
await builder.addDartFileEdit(file, (builder) {
27-
builder.addDeletion(range.token(questionMark));
28-
});
2936
}
3037

3138
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.

pkg/analysis_server/lib/src/services/correction/fix.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,9 @@ class DartFixKind {
444444
'dart.fix.remove.parenthesisInGetterInvocation',
445445
50,
446446
'Remove parentheses in getter invocation');
447-
static const REMOVE_QUESTION_MARK =
448-
FixKind('dart.fix.remove.questionMark', 50, "Remove the '?'");
447+
static const REMOVE_QUESTION_MARK = FixKind(
448+
'dart.fix.remove.questionMark', 50, "Remove the '?'",
449+
appliedTogetherMessage: 'Remove unnecessary question marks in file');
449450
static const REMOVE_THIS_EXPRESSION = FixKind(
450451
'dart.fix.remove.thisExpression', 50, 'Remove this expression',
451452
appliedTogetherMessage:

pkg/analysis_server/lib/src/services/correction/fix_internal.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,15 @@ class FixProcessor extends BaseProcessor {
10041004
],
10051005
)
10061006
],
1007+
LintNames.unnecessary_nullable_for_final_variable_declarations: [
1008+
FixInfo(
1009+
canBeAppliedToFile: true,
1010+
canBeBulkApplied: true,
1011+
generators: [
1012+
RemoveQuestionMark.newInstance,
1013+
],
1014+
)
1015+
],
10071016
LintNames.unnecessary_overrides: [
10081017
FixInfo(
10091018
canBeAppliedToFile: true,
@@ -1278,6 +1287,9 @@ class FixProcessor extends BaseProcessor {
12781287
LintNames.unnecessary_null_in_if_null_operators: [
12791288
RemoveIfNullOperator.newInstance,
12801289
],
1290+
LintNames.unnecessary_nullable_for_final_variable_declarations: [
1291+
RemoveQuestionMark.newInstance,
1292+
],
12811293
LintNames.unnecessary_overrides: [
12821294
RemoveMethodDeclaration.newInstance,
12831295
],

pkg/analysis_server/lib/src/services/linter/lint_names.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ class LintNames {
9595
static const String unnecessary_new = 'unnecessary_new';
9696
static const String unnecessary_null_in_if_null_operators =
9797
'unnecessary_null_in_if_null_operators';
98+
static const String unnecessary_nullable_for_final_variable_declarations =
99+
'unnecessary_nullable_for_final_variable_declarations';
98100
static const String unnecessary_overrides = 'unnecessary_overrides';
99101
static const String unnecessary_parenthesis = 'unnecessary_parenthesis';
100102
static const String unnecessary_string_interpolations =
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2021, 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:analysis_server/src/services/linter/lint_names.dart';
6+
import 'package:test_reflective_loader/test_reflective_loader.dart';
7+
8+
import 'bulk_fix_processor.dart';
9+
10+
void main() {
11+
defineReflectiveSuite(() {
12+
defineReflectiveTests(RemoveQuestionMarkTest);
13+
});
14+
}
15+
16+
@reflectiveTest
17+
class RemoveQuestionMarkTest extends BulkFixProcessorTest {
18+
@override
19+
String get lintCode =>
20+
LintNames.unnecessary_nullable_for_final_variable_declarations;
21+
22+
@override
23+
String get testPackageLanguageVersion => latestLanguageVersion;
24+
25+
Future<void> test_singleFile() async {
26+
await resolveTestCode('''
27+
class C {
28+
static final int? x = 0;
29+
static final int? y = 0;
30+
}
31+
''');
32+
await assertHasFix('''
33+
class C {
34+
static final int x = 0;
35+
static final int y = 0;
36+
}
37+
''');
38+
}
39+
}

pkg/analysis_server/test/src/services/correction/fix/bulk/test_all.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import 'remove_interpolation_braces_test.dart' as remove_interpolation_braces;
5151
import 'remove_method_declaration_test.dart' as remove_method_declaration;
5252
import 'remove_non_null_assertion_test.dart' as remove_non_null_assertion;
5353
import 'remove_operator_test.dart' as remove_operator;
54+
import 'remove_question_mark_test.dart' as remove_question_mark;
5455
import 'remove_this_expression_test.dart' as remove_this_expression;
5556
import 'remove_type_annotation_test.dart' as remove_type_annotation;
5657
import 'remove_unnecessary_const_test.dart' as remove_unnecessary_const;
@@ -115,6 +116,7 @@ void main() {
115116
remove_method_declaration.main();
116117
remove_non_null_assertion.main();
117118
remove_operator.main();
119+
remove_question_mark.main();
118120
remove_this_expression.main();
119121
remove_type_annotation.main();
120122
remove_unnecessary_const.main();

pkg/analysis_server/test/src/services/correction/fix/remove_question_mark_test.dart

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'fix_processor.dart';
1212
void main() {
1313
defineReflectiveSuite(() {
1414
defineReflectiveTests(RemoveQuestionMarkTest);
15+
defineReflectiveTests(UnnecessaryNullableForFinalVariableDeclarationsTest);
1516
});
1617
}
1718

@@ -94,3 +95,72 @@ class B with A {}
9495
''');
9596
}
9697
}
98+
99+
@reflectiveTest
100+
class UnnecessaryNullableForFinalVariableDeclarationsTest
101+
extends FixProcessorLintTest with WithNullSafetyMixin {
102+
@override
103+
FixKind get kind => DartFixKind.REMOVE_QUESTION_MARK;
104+
105+
@override
106+
String get lintCode => 'unnecessary_nullable_for_final_variable_declarations';
107+
108+
Future<void> test_const_field_static() async {
109+
await resolveTestCode('''
110+
class C {
111+
static const int? zero = 0;
112+
}
113+
''');
114+
await assertHasFix('''
115+
class C {
116+
static const int zero = 0;
117+
}
118+
''');
119+
}
120+
121+
Future<void> test_const_topLevelVariable() async {
122+
await resolveTestCode('''
123+
const int? zero = 0;
124+
''');
125+
await assertHasFix('''
126+
const int zero = 0;
127+
''');
128+
}
129+
130+
Future<void> test_final_field_static() async {
131+
await resolveTestCode('''
132+
class C {
133+
static final int? zero = 0;
134+
}
135+
''');
136+
await assertHasFix('''
137+
class C {
138+
static final int zero = 0;
139+
}
140+
''');
141+
}
142+
143+
Future<void> test_final_localVariable() async {
144+
await resolveTestCode('''
145+
void f() {
146+
final int? zero = 0;
147+
zero;
148+
}
149+
''');
150+
await assertHasFix('''
151+
void f() {
152+
final int zero = 0;
153+
zero;
154+
}
155+
''');
156+
}
157+
158+
Future<void> test_final_topLevelVariable() async {
159+
await resolveTestCode('''
160+
final int? zero = 0;
161+
''');
162+
await assertHasFix('''
163+
final int zero = 0;
164+
''');
165+
}
166+
}

0 commit comments

Comments
 (0)