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

+ no_wildcard_variable_uses #4396

Merged
merged 6 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -99,6 +99,7 @@ linter:
- no_logic_in_create_state
- no_runtimeType_toString
- no_self_assignments
- no_wildcard_variable_uses
- non_constant_identifier_names
- noop_primitive_operations
- null_check_on_nullable_type_parameter
Expand Down
2 changes: 2 additions & 0 deletions lib/src/rules.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ import 'rules/no_literal_bool_comparisons.dart';
import 'rules/no_logic_in_create_state.dart';
import 'rules/no_runtimeType_toString.dart';
import 'rules/no_self_assignments.dart';
import 'rules/no_wildcard_variable_uses.dart';
import 'rules/non_constant_identifier_names.dart';
import 'rules/noop_primitive_operations.dart';
import 'rules/null_check_on_nullable_type_parameter.dart';
Expand Down Expand Up @@ -341,6 +342,7 @@ void registerLintRules({bool inTestMode = false}) {
..register(NoopPrimitiveOperations())
..register(NoRuntimeTypeToString())
..register(NoSelfAssignments())
..register(NoWildcardVariableUses())
..register(NullCheckOnNullableTypeParameter())
..register(NullClosures())
..register(OmitLocalVariableTypes())
Expand Down
85 changes: 85 additions & 0 deletions lib/src/rules/no_wildcard_variable_uses.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// 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:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element.dart';

import '../analyzer.dart';
import '../util/ascii_utils.dart';

const _desc = r"Don't use wildcard parameters or variables.";

const _details = r'''
**DON'T** use wildcard parameters or variables.

pq marked this conversation as resolved.
Show resolved Hide resolved
In a future version of the Dart langauge, wildcard parameters and local
variables (e.g., named with just underscores like `_`, `__`, `___`, etc.) will
become non-binding. When that happens, existing code that uses wildcard
parameters or variables will break. In anticipation and to make adoption
easier, this lint disallows wildcard and variable parameter uses.
pq marked this conversation as resolved.
Show resolved Hide resolved


**BAD:**
```dart
var _ = 1;
print(_); // LINT
```

```dart
void f(int __) {
print(__); // LINT multiple underscores too
}
```

**GOOD:**
```dart
for (var _ in [1, 2, 3]) count++;
```

```dart
var [a, _, b, _] = [1, 2, 3, 4];
```
''';

class NoWildcardVariableUses extends LintRule {
static const LintCode code = LintCode(
'no_wildcard_variable_uses', 'The referenced identifier is a wildcard.',
correctionMessage: 'Use an identifier name that is not a wildcard.');

NoWildcardVariableUses()
: super(
name: 'no_wildcard_variable_uses',
description: _desc,
details: _details,
group: Group.errors);

@override
LintCode get lintCode => code;

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

class _Visitor extends SimpleAstVisitor {
final LintRule rule;

_Visitor(this.rule);

@override
void visitSimpleIdentifier(SimpleIdentifier node) {
var element = node.staticElement;
if (element is! LocalVariableElement && element is! ParameterElement) {
return;
}

if (node.name.isJustUnderscores) {
pq marked this conversation as resolved.
Show resolved Hide resolved
rule.reportLint(node);
}
}
}
2 changes: 2 additions & 0 deletions test/rules/all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import 'no_duplicate_case_values_test.dart' as no_duplicate_case_values;
import 'no_leading_underscores_for_local_identifiers_test.dart'
as no_leading_underscores_for_local_identifiers;
import 'no_self_assignments_test.dart' as no_self_assignments;
import 'no_wildcard_variable_uses_test.dart' as no_wildcard_variable_uses;
import 'non_adjacent_strings_in_list_test.dart' as no_adjacent_strings_in_list;
import 'non_constant_identifier_names_test.dart'
as non_constant_identifier_names;
Expand Down Expand Up @@ -202,6 +203,7 @@ void main() {
no_duplicate_case_values.main();
no_leading_underscores_for_local_identifiers.main();
no_self_assignments.main();
no_wildcard_variable_uses.main();
non_constant_identifier_names.main();
null_check_on_nullable_type_parameter.main();
null_closures.main();
Expand Down
115 changes: 115 additions & 0 deletions test/rules/no_wildcard_variable_uses_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// 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(NoWildcardVariableUsesTest);
});
}

@reflectiveTest
class NoWildcardVariableUsesTest extends LintRuleTest {
@override
String get lintRule => 'no_wildcard_variable_uses';

test_constructor() async {
await assertNoDiagnostics(r'''
class C {
C._();
m() {
print(C._);
print(C._());
}
}
''');
}

test_declaredIdentifier() async {
await assertNoDiagnostics(r'''
f() {
for (var _ in [1, 2, 3]) ;
}
''');
}

test_field() async {
await assertNoDiagnostics(r'''
class C {
int _ = 0;
m() {
print(_);
}
}
''');
}

test_getter() async {
await assertNoDiagnostics(r'''
class C {
int get _ => 0;
m() {
print(_);
}
}
''');
}

test_localVar() async {
await assertDiagnostics(r'''
f() {
var _ = 1;
print(_);
}
''', [
lint(27, 1),
]);
}

test_method() async {
await assertNoDiagnostics(r'''
class C {
String _() => '';
m() {
print(_);
print(_());
}
}
''');
}

test_param() async {
await assertDiagnostics(r'''
f(int __) {
print(__);
}
''', [
lint(20, 2),
]);
}

test_topLevelFunction() async {
await assertNoDiagnostics(r'''
String _() => '';

f() {
print(_);
print(_());
}
''');
}

test_topLevelGetter() async {
await assertNoDiagnostics(r'''
int get _ => 0;

f() {
print(_);
}
''');
}
}