Skip to content

Commit 3394fec

Browse files
pqCommit Queue
authored andcommitted
[lint] unecessary_ignore: skip generated files
Bug: #35234 Change-Id: I7ba59a32906b25c4343edff212cbac3acaa94e41 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/404721 Auto-Submit: Phil Quitslund <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 2dc6d31 commit 3394fec

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:analyzer/dart/analysis/features.dart';
99
import 'package:analyzer/dart/element/element.dart';
1010
import 'package:analyzer/error/error.dart';
1111
import 'package:analyzer/error/listener.dart';
12+
import 'package:analyzer/source/file_source.dart';
1213
import 'package:analyzer/src/context/source.dart';
1314
import 'package:analyzer/src/dart/analysis/analysis_options.dart';
1415
import 'package:analyzer/src/dart/analysis/file_analysis.dart';
@@ -355,7 +356,9 @@ class LibraryAnalyzer {
355356

356357
// This must happen after all other diagnostics have been computed but
357358
// before the list of diagnostics has been filtered.
358-
for (var fileAnalysis in _libraryFiles.values) {
359+
for (var fileAnalysis in _libraryFiles.values
360+
// Only validate non-generated files.
361+
.whereNot((f) => f.file.source.isGenerated)) {
359362
IgnoreValidator(
360363
fileAnalysis.errorReporter,
361364
fileAnalysis.errorListener.errors,
@@ -717,7 +720,7 @@ class LibraryAnalyzer {
717720
} else if (state is LibraryImportWithFile && !state.importedFile.exists) {
718721
var errorCode = state.isDocImport
719722
? WarningCode.URI_DOES_NOT_EXIST_IN_DOC_IMPORT
720-
: isGeneratedSource(state.importedSource)
723+
: state.importedSource.isGenerated
721724
? CompileTimeErrorCode.URI_HAS_NOT_BEEN_GENERATED
722725
: CompileTimeErrorCode.URI_DOES_NOT_EXIST;
723726
errorReporter.atNode(
@@ -1152,3 +1155,7 @@ extension on file_state.DirectiveUri {
11521155
return DirectiveUriImpl();
11531156
}
11541157
}
1158+
1159+
extension on FileSource {
1160+
bool get isGenerated => isGeneratedSource(this);
1161+
}

pkg/linter/test/rules/unnecessary_ignore_test.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ main() {
1010
defineReflectiveSuite(() {
1111
defineReflectiveTests(UnnecessaryIgnoreTest);
1212
defineReflectiveTests(UnnecessaryIgnoreDisabledTest);
13+
defineReflectiveTests(UnnecessaryIgnoreGeneratedFileTest);
1314
});
1415
}
1516

@@ -35,6 +36,29 @@ void f() {}
3536
}
3637
}
3738

39+
@reflectiveTest
40+
class UnnecessaryIgnoreGeneratedFileTest extends LintRuleTest {
41+
@override
42+
String get lintRule => 'unnecessary_ignore';
43+
44+
@override
45+
String get testFileName => 'test.g.dart';
46+
47+
test_file() async {
48+
await assertNoDiagnostics(r'''
49+
// ignore_for_file: unused_local_variable
50+
void f() {}
51+
''');
52+
}
53+
54+
test_line() async {
55+
await assertNoDiagnostics(r'''
56+
// ignore: unused_local_variable
57+
void f() {}
58+
''');
59+
}
60+
}
61+
3862
@reflectiveTest
3963
class UnnecessaryIgnoreTest extends LintRuleTest {
4064
@override

0 commit comments

Comments
 (0)