https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108012
Bug ID: 108012
Summary: Add fix-it hint for smartptr.foo which should be
smartptr->foo
Product: gcc
Version: 13.0
Status: UNCONFIRMED
Keywords: diagnostic
Severity: enhancement
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: redi at gcc dot gnu.org
Target Milestone: ---
Given:
struct S { int i; };
struct Ptr
{
S* operator->() const;
};
void func(S* rawptr, Ptr smartptr)
{
int i = rawptr.i;
int j = smartptr.i;
}
G++ helpfully points out your mistake in the first case, but not the second:
sp.cc: In function 'void func(S*, Ptr)':
sp.cc:10:18: error: request for member 'i' in 'rawptr', which is of pointer
type 'S*' (maybe you meant to use '->' ?)
10 | int i = rawptr.i;
| ^
sp.cc:11:20: error: 'struct Ptr' has no member named 'i'
11 | int j = smartptr.i;
| ^
Maybe the compiler could chek whether Ptr::operator->() exists, and if so, add
a similar "(maybe you meant to use '->' ?)" hint.
For bonus points, check whether the result of smartptr->i would actually
compile. Maybe the user meant to access a real member of the smart pointer
type, like a Ptr::get() or Ptr::reset() member, in which case changing it ->
would not make it any more correct.
Also, it looks like the "maybe you mean to use '->' ?" is not a real fix-it
hint, i.e. not affected by -fdiagnostics-parseable-fixits. Should it be a real
fix-it?