Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion tools/clang_tidy/lib/clang_tidy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class ClangTidy {
required io.Directory repoPath,
String checksArg = '',
bool lintAll = false,
bool fix = false,
StringSink? outSink,
StringSink? errSink,
}) :
Expand All @@ -56,6 +57,7 @@ class ClangTidy {
repoPath: repoPath,
checksArg: checksArg,
lintAll: lintAll,
fix: fix,
errSink: errSink,
),
_outSink = outSink ?? io.stdout,
Expand Down Expand Up @@ -207,7 +209,7 @@ class ClangTidy {
break;
case LintAction.lint:
_outSink.writeln('🔶 linting $relativePath');
jobs.add(command.createLintJob(checks));
jobs.add(command.createLintJob(checks, options.fix));
break;
case LintAction.skipThirdParty:
_outSink.writeln('🔷 ignoring $relativePath (third_party)');
Expand Down
4 changes: 3 additions & 1 deletion tools/clang_tidy/lib/src/command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,13 @@ class Command {
}

/// The job for the process runner for the lint needed for this command.
WorkerJob createLintJob(String? checks) {
WorkerJob createLintJob(String? checks, bool fix) {
final List<String> args = <String>[
filePath,
if (checks != null)
checks,
if (fix)
'--fix',
'--',
];
args.addAll(tidyArgs.split(' '));
Expand Down
12 changes: 11 additions & 1 deletion tools/clang_tidy/lib/src/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Options {
this.verbose = false,
this.checksArg = '',
this.lintAll = false,
this.fix = false,
this.errorMessage,
StringSink? errSink,
}) : checks = checksArg.isNotEmpty ? '--checks=$checksArg' : null,
Expand Down Expand Up @@ -58,6 +59,7 @@ class Options {
checksArg: options.wasParsed('checks') ? options['checks'] as String : '',
lintAll: io.Platform.environment['FLUTTER_LINT_ALL'] != null ||
options['lint-all'] as bool,
fix: options['fix'] as bool,
errSink: errSink,
);
}
Expand Down Expand Up @@ -91,6 +93,11 @@ class Options {
help: 'lint all of the sources, regardless of FLUTTER_NOLINT.',
defaultsTo: false,
)
..addFlag(
'fix',
help: 'Apply suggested fixes.',
defaultsTo: false,
)
..addFlag(
'verbose',
help: 'Print verbose output.',
Expand Down Expand Up @@ -134,6 +141,9 @@ class Options {
/// Whether all files should be linted.
final bool lintAll;

/// Whether checks should apply available fix-ups to the working copy.
final bool fix;

/// If there was a problem with the command line arguments, this string
/// contains the error message.
final String? errorMessage;
Expand All @@ -146,7 +156,7 @@ class Options {
_errSink.writeln(message);
}
_errSink.writeln(
'Usage: bin/main.dart [--help] [--lint-all] [--verbose] [--diff-branch]',
'Usage: bin/main.dart [--help] [--lint-all] [--fix] [--verbose] [--diff-branch]',
);
_errSink.writeln(_argParser.usage);
}
Expand Down
14 changes: 12 additions & 2 deletions tools/clang_tidy/test/clang_tidy_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,24 @@ Future<int> main(List<String> args) async {
expect(commands, isNotEmpty);
final Command command = commands.first;
expect(command.tidyPath, contains('clang/bin/clang-tidy'));
final WorkerJob job = command.createLintJob(null);
expect(job.command, <String>[
final WorkerJob jobNoFix = command.createLintJob(null, false);
expect(jobNoFix.command, <String>[
'../../buildtools/mac-x64/clang/bin/clang-tidy',
filePath,
'--',
'',
filePath,
]);

final WorkerJob jobWithFix = command.createLintJob(null, true);
expect(jobWithFix.command, <String>[
'../../buildtools/mac-x64/clang/bin/clang-tidy',
filePath,
'--fix',
'--',
'',
filePath,
]);
});

test('Command getLintAction flags third_party files', () async {
Expand Down