Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new unnecessary_late lint #3052

Merged
merged 4 commits into from
Dec 8, 2021
Merged
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
1 change: 1 addition & 0 deletions example/all.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ linter:
- unnecessary_final
- unnecessary_getters_setters
- unnecessary_lambdas
- unnecessary_late
- unnecessary_new
- unnecessary_null_aware_assignments
- unnecessary_null_checks
Expand Down
2 changes: 2 additions & 0 deletions lib/src/rules.dart
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ import 'rules/unnecessary_constructor_name.dart';
import 'rules/unnecessary_final.dart';
import 'rules/unnecessary_getters_setters.dart';
import 'rules/unnecessary_lambdas.dart';
import 'rules/unnecessary_late.dart';
import 'rules/unnecessary_new.dart';
import 'rules/unnecessary_null_aware_assignments.dart';
import 'rules/unnecessary_null_checks.dart';
Expand Down Expand Up @@ -376,6 +377,7 @@ void registerLintRules({bool inTestMode = false}) {
//..register(UnnecessaryGetters())
..register(UnnecessaryGettersSetters())
..register(UnnecessaryLambdas())
..register(UnnecessaryLate())
..register(UnnecessaryNullableForFinalVariableDeclarations())
..register(UnnecessaryNullChecks())
..register(UnnecessaryOverrides())
Expand Down
86 changes: 86 additions & 0 deletions lib/src/rules/unnecessary_late.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright (c) 2021, 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:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';

import '../analyzer.dart';

const _desc = r"Don't specify the `late` modifier when it is not needed.";

const _details = r'''

**DO** not specify the `late` modifier for top-level and static variables
when the declaration contains an initializer.

Top-level and static variables with initializers are already evaluated lazily
as if they are marked `late`.

**BAD:**
```dart
late String badTopLevel = '';
```

**GOOD:**
```dart
String goodTopLevel = '';
```

**BAD:**
```dart
class BadExample {
static late String badStatic = '';
}
```

**GOOD:**
```dart
class GoodExample {
late String goodStatic;
}
```
''';

class UnnecessaryLate extends LintRule {
UnnecessaryLate()
: super(
name: 'unnecessary_late',
description: _desc,
details: _details,
group: Group.style);

@override
void registerNodeProcessors(
NodeLintRegistry registry, LinterContext context) {
var visitor = _Visitor(this);
registry.addFieldDeclaration(this, visitor);
registry.addTopLevelVariableDeclaration(this, visitor);
}
}

class _Visitor extends SimpleAstVisitor<void> {
final LintRule rule;

_Visitor(this.rule);

@override
void visitFieldDeclaration(FieldDeclaration node) {
if (node.isStatic) {
_visitVariableDeclarations(node.fields);
}
}

@override
void visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) {
_visitVariableDeclarations(node.variables);
}

void _visitVariableDeclarations(VariableDeclarationList node) {
for (var variable in node.variables) {
if (variable.isLate && variable.initializer != null) {
rule.reportLint(variable);
}
}
}
}
23 changes: 23 additions & 0 deletions test_data/rules/unnecessary_late.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) 2021, 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.

// test w/ `dart test -N unnecessary_late`

late String unnecessaryTopLevelLate = ''; // LINT

late String necessaryTopLevelLate; // OK

String unnecessaryTopLevel = ''; // OK

class Test {
static late String unnecessaryStaticLate = ''; // LINT

static late String necessaryStaticLate; // OK

static String unnecessaryStatic = ''; // OK

void test() {
late String necessaryLocal = ''; // OK
}
}