Skip to content

Commit 6d61a79

Browse files
committed
Issue #92: renaming rule to avoid_unnecessary_return_variable
1 parent 89e940a commit 6d61a79

File tree

6 files changed

+31
-29
lines changed

6 files changed

+31
-29
lines changed

lib/analysis_options.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ custom_lint:
5656
- cyclomatic_complexity:
5757
max_complexity: 10
5858

59-
- dont_create_a_return_var
59+
- avoid_unnecessary_return_variable
6060
- double_literal_format
6161

6262
- function_lines_of_code:

lib/solid_lints.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import 'package:solid_lints/src/lints/avoid_global_state/avoid_global_state_rule
77
import 'package:solid_lints/src/lints/avoid_late_keyword/avoid_late_keyword_rule.dart';
88
import 'package:solid_lints/src/lints/avoid_non_null_assertion/avoid_non_null_assertion_rule.dart';
99
import 'package:solid_lints/src/lints/avoid_returning_widgets/avoid_returning_widgets_rule.dart';
10+
import 'package:solid_lints/src/lints/avoid_unnecessary_return_variable/avoid_unnecessary_return_variable_rule.dart';
1011
import 'package:solid_lints/src/lints/avoid_unnecessary_setstate/avoid_unnecessary_set_state_rule.dart';
1112
import 'package:solid_lints/src/lints/avoid_unnecessary_type_assertions/avoid_unnecessary_type_assertions_rule.dart';
1213
import 'package:solid_lints/src/lints/avoid_unnecessary_type_casts/avoid_unnecessary_type_casts_rule.dart';
1314
import 'package:solid_lints/src/lints/avoid_unrelated_type_assertions/avoid_unrelated_type_assertions_rule.dart';
1415
import 'package:solid_lints/src/lints/avoid_unused_parameters/avoid_unused_parameters_rule.dart';
1516
import 'package:solid_lints/src/lints/avoid_using_api/avoid_using_api_rule.dart';
1617
import 'package:solid_lints/src/lints/cyclomatic_complexity/cyclomatic_complexity_rule.dart';
17-
import 'package:solid_lints/src/lints/dont_create_a_return_var/dont_create_a_return_var_rule.dart';
1818
import 'package:solid_lints/src/lints/double_literal_format/double_literal_format_rule.dart';
1919
import 'package:solid_lints/src/lints/function_lines_of_code/function_lines_of_code_rule.dart';
2020
import 'package:solid_lints/src/lints/member_ordering/member_ordering_rule.dart';
@@ -68,7 +68,7 @@ class _SolidLints extends PluginBase {
6868
PreferEarlyReturnRule.createRule(configs),
6969
AvoidFinalWithGetterRule.createRule(configs),
7070
NamedParametersOrderingRule.createRule(configs),
71-
DontCreateAReturnVarRule.createRule(configs),
71+
AvoidUnnecessaryReturnVariableRule.createRule(configs),
7272
];
7373

7474
// Return only enabled rules

lib/src/lints/dont_create_a_return_var/dont_create_a_return_var_rule.dart renamed to lib/src/lints/avoid_unnecessary_return_variable/avoid_unnecessary_return_variable_rule.dart

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import 'package:analyzer/dart/ast/ast.dart';
22
import 'package:analyzer/dart/element/element2.dart';
33
import 'package:analyzer/error/listener.dart';
44
import 'package:custom_lint_builder/custom_lint_builder.dart';
5-
import 'package:solid_lints/src/lints/dont_create_a_return_var/visitors/dont_create_a_return_var_visitor.dart';
5+
import 'package:solid_lints/src/lints/avoid_unnecessary_return_variable/visitors/avoid_unnecessary_return_variable_visitor.dart';
66
import 'package:solid_lints/src/models/rule_config.dart';
77
import 'package:solid_lints/src/models/solid_lint_rule.dart';
88

9-
/// A `dont_create_a_return_var` rule which forbids returning an immutable
10-
/// variable if it can be rewritten in return statement itself.
9+
/// An `avoid_unnecessary_return_variable` rule which forbids returning
10+
/// an immutable variable if it can be rewritten in return statement itself.
1111
///
1212
/// See more here: https://github.com/solid-software/solid_lints/issues/92
1313
///
@@ -31,16 +31,18 @@ import 'package:solid_lints/src/models/solid_lint_rule.dart';
3131
/// }
3232
/// ```
3333
///
34-
class DontCreateAReturnVarRule extends SolidLintRule {
34+
class AvoidUnnecessaryReturnVariableRule extends SolidLintRule {
3535
/// This lint rule represents the error
3636
/// when unnecessary return variable statement is found
37-
static const lintName = 'dont_create_a_return_var';
37+
static const lintName = 'avoid_unnecessary_return_variable';
3838

39-
DontCreateAReturnVarRule._(super.config);
39+
AvoidUnnecessaryReturnVariableRule._(super.config);
4040

41-
/// Creates a new instance of [DontCreateAReturnVarRule]
41+
/// Creates a new instance of [AvoidUnnecessaryReturnVariableRule]
4242
/// based on the lint configuration.
43-
factory DontCreateAReturnVarRule.createRule(CustomLintConfigs configs) {
43+
factory AvoidUnnecessaryReturnVariableRule.createRule(
44+
CustomLintConfigs configs,
45+
) {
4446
final rule = RuleConfig(
4547
configs: configs,
4648
name: lintName,
@@ -49,7 +51,7 @@ Avoid creating unnecessary variable only for return.
4951
Rewrite the variable evaluation into return statement instead.""",
5052
);
5153

52-
return DontCreateAReturnVarRule._(rule);
54+
return AvoidUnnecessaryReturnVariableRule._(rule);
5355
}
5456

5557
@override
@@ -88,7 +90,7 @@ Rewrite the variable evaluation into return statement instead.""",
8890
final blockBody = statement.parent;
8991
if (blockBody == null) return;
9092

91-
final visitor = DontCreateAReturnVarVisitor(
93+
final visitor = AvoidUnnecessaryReturnVariableVisitor(
9294
returnVariableElement,
9395
statement,
9496
);

lib/src/lints/dont_create_a_return_var/visitors/dont_create_a_return_var_visitor.dart renamed to lib/src/lints/avoid_unnecessary_return_variable/visitors/avoid_unnecessary_return_variable_visitor.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import 'package:collection/collection.dart';
55

66
/// This visitor is searches all uses of a single local variable,
77
/// including variable declaration.
8-
class DontCreateAReturnVarVisitor extends RecursiveAstVisitor<void> {
8+
class AvoidUnnecessaryReturnVariableVisitor extends RecursiveAstVisitor<void> {
99
/// The problem expects that exactly 1 mention of return variable.
1010
/// VariableDeclarationStatement doesn't count when visiting SimpleIdentifier.
1111
/// Any other amount of variable mentions implies that it is used somewhere
@@ -28,8 +28,8 @@ class DontCreateAReturnVarVisitor extends RecursiveAstVisitor<void> {
2828
/// Returns statement of local variable declaration
2929
VariableDeclaration? get variableDeclaration => _variableDeclaration;
3030

31-
/// Creates a new instance of [DontCreateAReturnVarVisitor].
32-
DontCreateAReturnVarVisitor(
31+
/// Creates a new instance of [AvoidUnnecessaryReturnVariableVisitor].
32+
AvoidUnnecessaryReturnVariableVisitor(
3333
this._returnVariableElement,
3434
this._returnStatement,
3535
);

lint_test/analysis_options.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,4 @@ custom_lint:
8989
- required
9090
- nullable
9191
- default
92-
- dont_create_a_return_var
92+
- avoid_unnecessary_return_variable

lint_test/dont_create_a_return_var_test.dart

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// ignore_for_file: unused_local_variable, newline_before_return, no_empty_block
22

3-
/// Test the dont_create_a_return_var.
3+
/// Test the avoid_unnecessary_return_variable.
44
/// Good code, trivial case.
55
int returnVarTestGoodTrivial() {
66
return 1;
77
}
88

9-
/// Test the dont_create_a_return_var.
9+
/// Test the avoid_unnecessary_return_variable.
1010
/// Returning mutable variable should not trigger the lint.
1111
int returnVarTestReturnMutable() {
1212
var a = 1;
@@ -19,7 +19,7 @@ int returnVarTestReturnParameter(int param) {
1919
return param;
2020
}
2121

22-
/// Test the dont_create_a_return_var.
22+
/// Test the avoid_unnecessary_return_variable.
2323
/// Caching mutable variable value.
2424
/// Unpredictable: may be useful to cache value
2525
/// before operation can change it.
@@ -31,7 +31,7 @@ int returnVarTestCachedMutable() {
3131
return result;
3232
}
3333

34-
/// Test the dont_create_a_return_var.
34+
/// Test the avoid_unnecessary_return_variable.
3535
/// Caching mutable variable value, but return goes
3636
/// right after declaration, which makes it bad.
3737
int returnVarTestReturnFollowsDeclaration() {
@@ -40,11 +40,11 @@ int returnVarTestReturnFollowsDeclaration() {
4040

4141
//Some comment here
4242

43-
//expect_lint: dont_create_a_return_var
43+
//expect_lint: avoid_unnecessary_return_variable
4444
return result;
4545
}
4646

47-
/// Test the dont_create_a_return_var.
47+
/// Test the avoid_unnecessary_return_variable.
4848
/// Caching another method result.
4949
/// Unpredictable: may be useful to cache value
5050
/// before operation can change it.
@@ -56,7 +56,7 @@ int returnVarTestCachedAnotherMethodResult() {
5656
return result;
5757
}
5858

59-
/// Test the dont_create_a_return_var.
59+
/// Test the avoid_unnecessary_return_variable.
6060
/// Caching value of object's field.
6161
/// Unpredictable: may be useful to cache value
6262
/// before operation can change it.
@@ -68,7 +68,7 @@ int returnVarTestCachedObjectField() {
6868
return result;
6969
}
7070

71-
/// Test the dont_create_a_return_var.
71+
/// Test the avoid_unnecessary_return_variable.
7272
/// Good: variable is created not only for return
7373
/// but is used in following expressions as well.
7474
int returnVarTestUsedVariable() {
@@ -79,16 +79,16 @@ int returnVarTestUsedVariable() {
7979
return result;
8080
}
8181

82-
/// Test the dont_create_a_return_var.
82+
/// Test the avoid_unnecessary_return_variable.
8383
/// Bad code, trivial example.
8484
int returnVarTestBadTrivial() {
8585
final result = 1;
8686

87-
//expect_lint: dont_create_a_return_var
87+
//expect_lint: avoid_unnecessary_return_variable
8888
return result;
8989
}
9090

91-
/// Test the dont_create_a_return_var.
91+
/// Test the avoid_unnecessary_return_variable.
9292
/// Bad code: result expression is immutable,
9393
/// so can be written in return statement directly.
9494
int returnVarTestBadImmutableExpression() {
@@ -103,7 +103,7 @@ int returnVarTestBadImmutableExpression() {
103103
testObj.finalField;
104104
_doNothing();
105105

106-
//expect_lint: dont_create_a_return_var
106+
//expect_lint: avoid_unnecessary_return_variable
107107
return result;
108108
}
109109

0 commit comments

Comments
 (0)