|
| 1 | +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 6 | + |
| 7 | +import '../rule_test_support.dart'; |
| 8 | + |
| 9 | +main() { |
| 10 | + defineReflectiveSuite(() { |
| 11 | + defineReflectiveTests(NoSelfAssignmentsTest); |
| 12 | + }); |
| 13 | +} |
| 14 | + |
| 15 | +@reflectiveTest |
| 16 | +class NoSelfAssignmentsTest extends LintRuleTest { |
| 17 | + @override |
| 18 | + String get lintRule => 'no_self_assignments'; |
| 19 | + |
| 20 | + test_fieldAssignment() async { |
| 21 | + await assertDiagnostics(r''' |
| 22 | +class C { |
| 23 | + int x = 5; |
| 24 | +
|
| 25 | + C(int x) { |
| 26 | + x = x; |
| 27 | + } |
| 28 | +} |
| 29 | +''', [lint(41, 5)]); |
| 30 | + } |
| 31 | + |
| 32 | + test_fieldAssignmentDifferentVar() async { |
| 33 | + await assertNoDiagnostics(r''' |
| 34 | +class C { |
| 35 | + int x = 5; |
| 36 | +
|
| 37 | + C(int y) { |
| 38 | + x = y; |
| 39 | + } |
| 40 | +} |
| 41 | +'''); |
| 42 | + } |
| 43 | + |
| 44 | + test_fieldAssignmentExplicit() async { |
| 45 | + await assertNoDiagnostics(r''' |
| 46 | +class C { |
| 47 | + int x = 5; |
| 48 | +
|
| 49 | + C(int x) { |
| 50 | + this.x = x; |
| 51 | + } |
| 52 | +} |
| 53 | +'''); |
| 54 | + } |
| 55 | + |
| 56 | + test_fieldAssignmentExplicitSameVar() async { |
| 57 | + await assertDiagnostics(r''' |
| 58 | +class C { |
| 59 | + int x = 5; |
| 60 | +
|
| 61 | + void update(C other) { |
| 62 | + other.x = other.x; |
| 63 | + } |
| 64 | +} |
| 65 | +''', [lint(53, 17)]); |
| 66 | + } |
| 67 | + |
| 68 | + test_fieldAssignmentThisAndDifferentTarget() async { |
| 69 | + await assertNoDiagnostics(r''' |
| 70 | +class C { |
| 71 | + int x = 5; |
| 72 | +
|
| 73 | + void update(C other) { |
| 74 | + this.x = other.x; |
| 75 | + } |
| 76 | +} |
| 77 | +'''); |
| 78 | + } |
| 79 | + |
| 80 | + test_fieldAssignmentDifferentTargets() async { |
| 81 | + await assertNoDiagnostics(r''' |
| 82 | +class C { |
| 83 | + String hello = 'ok'; |
| 84 | +} |
| 85 | +
|
| 86 | +void test(C one, C two) { |
| 87 | + one.hello = two.hello; |
| 88 | +} |
| 89 | +'''); |
| 90 | + } |
| 91 | + |
| 92 | + test_fieldInitialization() async { |
| 93 | + await assertNoDiagnostics(r''' |
| 94 | +class C { |
| 95 | + int x; |
| 96 | +
|
| 97 | + C(int x) : x = x; |
| 98 | +} |
| 99 | +'''); |
| 100 | + } |
| 101 | + |
| 102 | + test_propertyAssignment() async { |
| 103 | + await assertDiagnostics(r''' |
| 104 | +class C { |
| 105 | + int _x = 5; |
| 106 | +
|
| 107 | + int get x => _x; |
| 108 | +
|
| 109 | + set x(int x) { |
| 110 | + _x = x; |
| 111 | + } |
| 112 | +
|
| 113 | + void example() { |
| 114 | + x = x; |
| 115 | + } |
| 116 | +} |
| 117 | +''', [lint(102, 5)]); |
| 118 | + } |
| 119 | + |
| 120 | + test_classMemberAssignment() async { |
| 121 | + await assertDiagnostics(r''' |
| 122 | +class C { |
| 123 | + static String foo = "foo"; |
| 124 | +} |
| 125 | +
|
| 126 | +void main() { |
| 127 | + C.foo = C.foo; |
| 128 | +} |
| 129 | +''', [lint(58, 13)]); |
| 130 | + } |
| 131 | + |
| 132 | + test_classMemberAssignmentUnrelated() async { |
| 133 | + await assertNoDiagnostics(r''' |
| 134 | +class C { |
| 135 | + static String foo = "foo"; |
| 136 | +} |
| 137 | +
|
| 138 | +void main() { |
| 139 | + String foo; |
| 140 | + foo = C.foo; // OK |
| 141 | + print(foo); |
| 142 | +} |
| 143 | +'''); |
| 144 | + } |
| 145 | +} |
0 commit comments