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