-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Fix for readability-redundant-member-init leaves non-compilable code #42928
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
assigned to @pepsiman |
Clang-tidy checks produce fixes that sometimes need to be cleaned up to remove redundant colons or commas. The cleanup is performed after applying the fixes. clang-tidy 9 performs this cleanup correctly: $ cat test.cpp class Foo { private: $ clang-tidy-9 --checks=readability-redundant-member-init test.cpp --fix -- $ cat test.cpp class Foo { private: Which tool are you using to apply the fixes? |
Thanks, I was not aware that some additional cleanup is taking place. Is it performed by clang-format, or by some logic in clang-tidy itself? I'm using ReSharper C++, which just applies the fixit provided by clang-tidy. In this case the text edit is:
That said, it should be possible to change the fixit provided by this check so that no additional cleanup is necessary. This would help all external tools that consume clang-tidy fixits. |
The cleanup is implemented in the clang::format::cleanupAroundReplacements function. clang-tidy calls it here: clang-tidy/ClangTidy.cpp:205. Cleanup is done separately from producing the fixes, since it may become necessary when applying multiple independent fixes together, e.g. consider this code: ... If three different checks independently try to remove initializers for A similar problem exists for inserting/removing #include directives. We can't clean up replacements when exporting them, because they are grouped by diagnostic and can be applied one by one. The only good solution is for the tool that applies the fix to clean up code. If ReSharper doesn't do this, it's wrong. It should call clang::format::cleanupAroundReplacements before applying replacements. |
For context, ReSharper/CLion run clang-tidy in an external process, in order to:
It is also desirable to be able to apply a fix for a specific single inspection from the IDE. This means we can't use cleanupAroundReplacements with the current clang-tidy CLI. To do that we'd need to teach the clang-tidy to apply a single fix (--line-filter is insufficient for that), and to print the resulting replacements together with replacements from cleanupAroundReplacements instead of applying them to the physical file. An alternative check implementation would output a single check result for a given initialization list which would contain several fixes for all the redundant initializers (so that additional cleanup would not be necessary), but I do understand that the current design is simpler. |
The clang-apply-replacements tool can be used to apply a fix and cleanup. |
clang-apply-replacements is not really useful to us, as we'd like to be able to apply replacements inside the IDE itself. The usual workflow after applying a fix in an IDE is to change the text in the IDE buffer, not the physical file itself. The user can then evaluate the changes, and save the file when he wants to or revert the changes back. |
It seems like the best workaround is to duplicate what 'cleanupAroundReplacements' is doing (copy-paste, rewrite to C#; it's open-source after all) in ReSharper and hope this function does not change too often (which is probably the case). |
There are multiple options here. In no particular order (and without any guarantees that these would actually work or would be desired features for the relevant tools):
|
Extended Description
With clang-tidy 9 apply fix to the example from the check documentation:
class Foo {
public:
Foo() : s() {}
private:
std::string s;
};
Result:
class Foo {
public:
Foo() : {}
private:
std::string s;
};
The text was updated successfully, but these errors were encountered: