[patch] Improve diagnostic on default-initializing const variables (PR20208)

Nico Weber thakis at chromium.org
Tue Jul 22 09:18:18 PDT 2014


>
> Ideas? Easiest would be to put the fixit on a note, but the note would
>> just repeat the snippet from the diag itself, which isn't great.
>>
>
> We do that in a lot of cases; this wouldn't be so bad, and it'd be safer.
> The current approach is also workable, because we never make a choice based
> on whether default initialization of a variable is valid, but it seems
> fragile.
>
> The nice thing about putting it in a separate note is that you can point
> the caret directly at the place where the fixit text should be inserted,
> while still having the main diagnostic point at the entity being
> initialized; that tends to make fixits much more obvious.
>

Ok, here's a patch that adds the fixit on a note instead. The code change
is now very simple. The diagnostic looks like this:

test.cc:2:9: error: default initialization of an object of const type
'const A' without a user-provided default constructor
const A aasdf;
        ^
test.cc:2:14: note: add an explicit initializer to initialize 'aasdf'
const A aasdf;
             ^
              = {}
1 error generated.

Maybe it should be " add an explicit initializer for 'aasdf'"? That's
shorter, but it sounds too confident maybe.

The test case updates are mostly boring.
In cxx1y-variable-templates_in_class.cpp, the variable name for a template
is printed as "…to initialize 'b<int, int>'" with a note "in instantiation
of". I found that a bit surprising, but it matches the diagnostic on the
same line, so I suppose that's ok.

Nico
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140722/4f6d8dd4/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: clang-init.patch
Type: application/octet-stream
Size: 11888 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140722/4f6d8dd4/attachment.obj>


More information about the cfe-commits mailing list