Skip to content

[Clang] Add a fix-it hint for ++this -> ++*this #93066

@Sirraide

Description

@Sirraide

There was a suggestion to add a fix-it hint that would correct ++this to ++*this following a discussion about #92439 now that we diagnose the former even in dependent contexts. From what I can tell, this would entail adding a fix-it (probably in CheckForModifiableLvalue in SemaExpr.cpp) that inserts a * before the expression, if

  • it is of pointer type, and
  • the expression itself is not a modifiable lvalue, and
  • the pointee type is modifiable (e.g. don’t suggest ++this -> ++*this in a const member function).

Notably, doing this in CheckForModifiableLvalue would cover more cases than just ++. Possible test cases I can think of would be:

void f(int* a, int* const b, const int* const c, __UINTPTR_TYPE__ d) {
    (int*)d = 4; // Suggest a fixit; `(int*)d` is a prvalue.
    ++a;         // No fixit; this is fine.
    ++b;         // Suggest a fixit; `b` is const but `*b` isn’t.
    ++c;         // No fixit; `++c` is invalid, but so is `++*c`.
}

struct S {
    void f() { 
        // Suggest a fixit. `++*this` is probably what the user meant.
        //
        // Note: if this is in a template, then we generally won’t have
        // any way of knowing whether there even is an overload of `++` 
        // for this type, but it makes sense to assume there is because
        // it’s unlikely that a user would just try and increment it any-
        // way if that was the case.
        ++this; 
    }

    void g() const { 
        // No fixit here. Of course, one *could* define an overload of
        // `++` that is `const`, but doing so would be rather confusing
        // considering what this operator usually does, so most people
        // won’t do that and suggesting a `*` therefore would just lead
        // to a lot of false positives.
        ++this; 
    }
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerenhancementImproving things as opposed to bug fixing, e.g. new or missing feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions