Added an option to control whether to apply the fixes found in notes attached to clang tidy errors or not.
Diagnostics may contain multiple notes each offering different ways to fix the issue, for that reason the default behaviour should be to not look at fixes found in notes.
Instead offer up all the available fix-its in the output but don't try to apply the first one unless -fix-notes is supplied.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
This is very much a work in progress
Another direction I was thinking was only apply the fixes found in notes if there is exactly one fix attached to the notes in a diagnostic, instead of just applying the first one we find
clang-tools-extra/clang-tidy/ClangTidy.cpp | ||
---|---|---|
106 | It'll be reasonable to use member initialization for TotalFixes, AppliedFixes, WarningsAsErrors. | |
clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp | ||
266–267 | It'll be reasonable to use member initialization for LastErrorRelatesToUserCode, LastErrorPassesLineFilter, LastErrorWasIgnored. |
Ah, sorry, this fell to the bottom of my review list because the title still says [WIP] and so I thought there was more work being done on it. You may want to remove that bit from the title.
I was wondering the same thing here myself. If there's exactly one fix, then it's unambiguous as to what behavior you get. One (minor) concern I have about the current approach is with nondeterminism in diagnostic ordering where different runs may pick different fixes for the same code. I don't *think* we have any instances of this in Clang or clang-tidy, but users can add their own plugins (for instance to the clang static analyzer) that may introduce some small risk there. Do you have a reason why you picked one approach over the other?
clang-tools-extra/clang-tidy/ClangTidy.h | ||
---|---|---|
103 | I think you should add some comments explaining the difference between Fix and AnyFix because it's not super clear. Alternatively, if these are strongly related, should this be an enumeration rather than two boolean parameters? e.g., enum FixitBehavior { NoFixes, ApplyNonNoteFixes, ApplyAllFixes }; or some such (note, this suggestion may or may not make sense, I'm still reviewing). | |
clang-tools-extra/test/clang-tidy/checkers/misc-definitions-in-headers.hpp | ||
1 | Hmm, should that be --fix-notes? (Note the number of dashes, which differs from the documentation) | |
clang-tools-extra/test/clang-tidy/infrastructure/alternative-fixes.cpp | ||
1 | Same question here as above. |
Use enum for handling fix behaviour.
Fix tests using --fix-notes instead of -fix-notes.
Part of the reason for this approach is from this bug report https://bugs.llvm.org/show_bug.cgi?id=47971, where its pointed out that clang diags gets fix-its added in notes if the fixes will change behaviour or they aren't sure its going to actually fix the issue.
As clang-tidy also applies fixes reported from clang, it is wise to adhere to a similar level caution.
Agreed, but I don't think the current behavior in this patch is cautious enough. I like that we have to specify "please apply fixits from notes" explicitly. I think that's the right behavior we want. But if there are multiple fixits attached to the notes for a diagnostic, I don't think we should assume that the first fix-it is the correct one to apply -- I think the user should have to manually pick the variant they want in that case. So I think the behavior should be to apply the fixits from notes if there's only a single fixit to be applied. WDYT?
I can agree with that, One thing I like about using clangd is it will show you all available fixes, letting you pick and choose.
clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h | ||
---|---|---|
231 | Comment seems stale. | |
clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp | ||
186 ↗ | (On Diff #313272) | Should this change be a separate patch? |
clang-tools-extra/clang-tidy/readability/InconsistentDeclarationParameterNameCheck.cpp | ||
222 ↗ | (On Diff #313272) | Same for these changes? |
clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp | ||
501 | Might as well fix this clang-format warning. | |
clang-tools-extra/docs/clang-tidy/index.rst | ||
176 | This sort of reads like it may apply fixes from multiple notes. How about: If a warning has no fix, but a single fix can be found through an associated diagnostic note, apply the fix. Specifying this flag will implicitly enable the '--fix' flag. (Note, I also quietly switched from using `` to using '' for quoting -- that seems to be the consistent style here.) |
clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp | ||
---|---|---|
186 ↗ | (On Diff #313272) | It's either modify the check to put the fix in the warning, or modify the test so that fix notes is specified. |
clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp | ||
501 | I think this is a discrepancy with the clang format in using (truck) and the pre merge. Mine isn't complaining about format |
I think you should add some comments explaining the difference between Fix and AnyFix because it's not super clear. Alternatively, if these are strongly related, should this be an enumeration rather than two boolean parameters? e.g., enum FixitBehavior { NoFixes, ApplyNonNoteFixes, ApplyAllFixes }; or some such (note, this suggestion may or may not make sense, I'm still reviewing).