Custom FixItHint not being reported

Greetings everyone,

I have an problem with a custom clang-tidy check that looks for “forbidden” using namespace directives (identical to the google-build-namespaces check, but with configuration options for allowing certain namespaces or allowing the directive inside a function). The diagnostic message that identifies where the using namespace is occurring is reported correctly. However, the fixit insertion hints I provide for prepending type and reference expressions with the imported namespace are not reported. In other words, I expect to see a fixit hint indicating that cout should be renamed std::cout since using namespace std; will be removed; none of those hints are being displayed.

I’ve ensured the insertion locations are valid and not inside macros. I think I know what’s happening, but I haven’t traced the code path into Clang far enough to confirm it: because the fixit insertion locations are not contained by the initial range reported to diag (the actual using namespace directive source range), the hints are being discarded. I could understand this happening since changing code outside of the “problem” range could be construed as an out-of-scope conflict. (“Why are you changing unrelated code?”)

Does this sound like what’s happening, or is it likely there is something else going on? I know this can be a pain to diagnose without code – my apologies for that up front.

FixItHints hints outside the ‘reported range’ are not discarded (try -fix).
The diagnostic you see will contain the location passed to diag, and any SourceRange you streamed into the diagnostic. For FixItHints, their annotations should be visible on the line/lines that are printed during execution. Any FixItHint outside of that will not be shown this way, but still be applied when -fix is supplied (or in editors). For example, Clang-Tidy checks that insert headers will not print that they insert the header when the diagnostic is issued.

1 Like

Oh, interesting! I didn’t even test it to see if this would occur. I just assumed since the fixit hints didn’t display, they were being discarded. I’ll give this a shot once I’m able to do so. Thanks!