Skip to content

Commit 4b3c8a3

Browse files
kallentuCommit Queue
authored andcommitted
[linter] Dot shorthands: Update exhaustive_cases lint.
Added support for the lint to fire on dot shorthand properties in switch cases. Tests added and passing. Bug: #60905, #60893 Change-Id: I475f79db547144817fecd4936616d959144b7404 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/434520 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Kallen Tu <[email protected]>
1 parent cca544b commit 4b3c8a3

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

pkg/linter/lib/src/rules/exhaustive_cases.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ class _Visitor extends SimpleAstVisitor<void> {
6868
if (variable is VariableElement) {
6969
enumConstants.remove(variable.computeConstantValue());
7070
}
71+
} else if (expression is DotShorthandPropertyAccess) {
72+
var variable = expression.propertyName.element.variableElement;
73+
if (variable is VariableElement) {
74+
enumConstants.remove(variable.computeConstantValue());
75+
}
7176
}
7277
if (member is SwitchDefault) {
7378
return;

pkg/linter/test/rules/exhaustive_cases_test.dart

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,102 @@ void s(Subclassed e) {
252252

253253
@reflectiveTest
254254
class ExhaustiveCasesTest extends BaseExhaustiveCasesTest {
255+
Future<void> test_dotShorthands_enumLike() async {
256+
await assertDiagnostics(
257+
r'''
258+
class E {
259+
final int i;
260+
const E._(this.i);
261+
262+
static const e = E._(1);
263+
static const f = E._(2);
264+
static const g = E._(3);
265+
}
266+
267+
void fn(E e) {
268+
switch (e) {
269+
case .e:
270+
break;
271+
case .f:
272+
}
273+
}
274+
''',
275+
[lint(148, 10)],
276+
);
277+
}
278+
279+
Future<void> test_dotShorthands_enumLike_default_ok() async {
280+
await assertNoDiagnostics(r'''
281+
class E {
282+
final int i;
283+
const E._(this.i);
284+
285+
static const e = E._(1);
286+
static const f = E._(2);
287+
static const g = E._(3);
288+
}
289+
290+
void fn(E e) {
291+
switch (e) {
292+
case .e:
293+
break;
294+
default:
295+
break;
296+
}
297+
}
298+
''');
299+
}
300+
301+
Future<void> test_dotShorthands_notEnumLike_ok() async {
302+
await assertNoDiagnostics(r'''
303+
class TooFew {
304+
const TooFew._();
305+
306+
static const e = TooFew._();
307+
}
308+
309+
void t(TooFew e) {
310+
switch (e) {
311+
case TooFew.e:
312+
}
313+
}
314+
315+
class PublicCons {
316+
const PublicCons();
317+
static const e = PublicCons();
318+
static const f = PublicCons();
319+
}
320+
321+
void p(PublicCons e) {
322+
switch (e) {
323+
case .e:
324+
}
325+
}
326+
''');
327+
}
328+
329+
Future<void> test_dotShorthands_notEnumLike_subclassed_ok() async {
330+
await assertNoDiagnostics(r'''
331+
class Subclassed {
332+
const Subclassed._();
333+
334+
static const e = Subclassed._();
335+
static const f = Subclassed._();
336+
static const g = Subclassed._();
337+
}
338+
339+
class Subclass extends Subclassed {
340+
Subclass() : super._();
341+
}
342+
343+
void s(Subclassed e) {
344+
switch (e) {
345+
case .e:
346+
}
347+
}
348+
''');
349+
}
350+
255351
test_enum_ok() async {
256352
await assertDiagnostics(actualEnumSource, [
257353
error(CompileTimeErrorCode.NON_EXHAUSTIVE_SWITCH_STATEMENT, 52, 6),

0 commit comments

Comments
 (0)