Skip to content

Commit f0f42de

Browse files
committed
Issue #92, new rule dont_create_a_return_var:
- added rule class (no rule implementation) - added test cases
1 parent ee3e27d commit f0f42de

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed

lib/solid_lints.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import 'package:solid_lints/src/lints/avoid_unrelated_type_assertions/avoid_unre
1414
import 'package:solid_lints/src/lints/avoid_unused_parameters/avoid_unused_parameters_rule.dart';
1515
import 'package:solid_lints/src/lints/avoid_using_api/avoid_using_api_rule.dart';
1616
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';
1718
import 'package:solid_lints/src/lints/double_literal_format/double_literal_format_rule.dart';
1819
import 'package:solid_lints/src/lints/function_lines_of_code/function_lines_of_code_rule.dart';
1920
import 'package:solid_lints/src/lints/member_ordering/member_ordering_rule.dart';
@@ -67,6 +68,7 @@ class _SolidLints extends PluginBase {
6768
PreferEarlyReturnRule.createRule(configs),
6869
AvoidFinalWithGetterRule.createRule(configs),
6970
NamedParametersOrderingRule.createRule(configs),
71+
DontCreateAReturnVarRule.createRule(configs),
7072
];
7173

7274
// Return only enabled rules
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import 'package:analyzer/error/listener.dart';
2+
import 'package:custom_lint_builder/custom_lint_builder.dart';
3+
import 'package:solid_lints/src/models/rule_config.dart';
4+
import 'package:solid_lints/src/models/solid_lint_rule.dart';
5+
6+
/// A `dont_create_a_return_var` rule which forbids returning an immutable
7+
/// variable if it can be rewritten in return statement itself.
8+
///
9+
/// See more here: https://github.com/solid-software/solid_lints/issues/92
10+
///
11+
/// ### Example
12+
///
13+
/// #### BAD:
14+
///
15+
/// ```dart
16+
/// void x() {
17+
/// final y = 1;
18+
///
19+
/// return y;
20+
/// }
21+
/// ```
22+
///
23+
/// #### GOOD:
24+
///
25+
/// ```dart
26+
/// void x() {
27+
/// return 1;
28+
/// }
29+
/// ```
30+
///
31+
class DontCreateAReturnVarRule extends SolidLintRule {
32+
/// This lint rule represents the error
33+
/// when unnecessary return variable statement is found
34+
static const lintName = 'dont_create_a_return_var';
35+
36+
DontCreateAReturnVarRule._(super.config);
37+
38+
/// Creates a new instance of [DontCreateAReturnVarRule]
39+
/// based on the lint configuration.
40+
factory DontCreateAReturnVarRule.createRule(CustomLintConfigs configs) {
41+
final rule = RuleConfig(
42+
configs: configs,
43+
name: lintName,
44+
problemMessage: (_) => """
45+
Avoid creating unnecessary variable only for return.
46+
Rewrite the variable evaluation into return statement instead.""",
47+
);
48+
49+
return DontCreateAReturnVarRule._(rule);
50+
}
51+
52+
53+
@override
54+
void run(
55+
CustomLintResolver resolver,
56+
ErrorReporter reporter,
57+
CustomLintContext context,
58+
) {
59+
// TODO: implement run
60+
}
61+
62+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// ignore_for_file: unused_local_variable
2+
3+
/// Test the dont_create_a_return_var.
4+
/// Good code, trivial case.
5+
int returnVarTestGoodTrivial() {
6+
return 1;
7+
}
8+
9+
/// Test the dont_create_a_return_var.
10+
/// Returning mutable variable should not trigger the lint.
11+
int returnVarTestReturnMutable() {
12+
var a = 1;
13+
a++;
14+
15+
return a;
16+
}
17+
18+
/// Test the dont_create_a_return_var.
19+
/// Caching mutable variable value.
20+
/// Unpredictable: may be useful to cache value
21+
/// before operation can change it.
22+
int returnVarTestCachedMutable() {
23+
var a = 1;
24+
final result = a;
25+
_doNothing();
26+
27+
return result;
28+
}
29+
30+
/// Test the dont_create_a_return_var.
31+
/// Caching another method result.
32+
/// Unpredictable: may be useful to cache value
33+
/// before operation can change it.
34+
int returnVarTestCachedAnotherMethodResult() {
35+
var a = 1;
36+
final result = _testValueEval();
37+
_doNothing();
38+
39+
return result;
40+
}
41+
42+
/// Test the dont_create_a_return_var.
43+
/// Caching value of object's field.
44+
/// Unpredictable: may be useful to cache value
45+
/// before operation can change it.
46+
int returnVarTestCachedObjectField() {
47+
final obj = _TestClass();
48+
final result = obj.varField;
49+
_doNothing();
50+
51+
return result;
52+
}
53+
54+
/// Test the dont_create_a_return_var.
55+
/// Good: variable is created not only for return
56+
/// but is used in following expressions as well.
57+
int returnVarTestUsedVariable() {
58+
var a = 1;
59+
final result = 2;
60+
a += result;
61+
62+
return result;
63+
}
64+
65+
/// Test the dont_create_a_return_var.
66+
/// Bad code, trivial example.
67+
int returnVarTestBadTrivial() {
68+
final result = 1;
69+
70+
//expect_lint: dont_create_a_return_var
71+
return result;
72+
}
73+
74+
/// Test the dont_create_a_return_var.
75+
/// Bad code: result expression is immutable,
76+
/// so can be written in return statement directly.
77+
int returnVarTestBadImmutableExpression() {
78+
const constLocal = 1;
79+
final finalLocal = 1;
80+
final testObj = _TestClass();
81+
final result =
82+
constLocal
83+
+ finalLocal
84+
+ 1 //const literal
85+
+ _TestClass.constValue
86+
+ _TestClass.finalValue
87+
+ testObj.finalField;
88+
_doNothing();
89+
90+
//expect_lint: dont_create_a_return_var
91+
return result;
92+
}
93+
94+
int _testValueEval() {
95+
return 1;
96+
}
97+
98+
/// This method is a placeholder for unpredictable behaviour
99+
/// which can potentially change any mutable variables
100+
//ignore: no_empty_block
101+
void _doNothing() { }
102+
103+
//ignore: prefer_match_file_name
104+
class _TestClass {
105+
static const constValue = 1;
106+
static final finalValue = 1;
107+
//ignore: member_ordering
108+
final finalField = 1;
109+
var varField = 1;
110+
}

0 commit comments

Comments
 (0)