[clang] [clang] Fix-it hint for `++this` -> `++*this` when deref is modifiable (PR #94159)

via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 11 10:07:13 PDT 2024


================
@@ -13273,6 +13273,22 @@ enum {
   ConstUnknown,  // Keep as last element
 };
 
+static void MaybeSuggestDerefFixIt(Sema &S, const Expr *E, SourceLocation Loc) {
+  ExprResult Deref;
+  Expr *TE = const_cast<Expr *>(E);
+  {
+    Sema::TentativeAnalysisScope Trap(S);
+    Deref = S.ActOnUnaryOp(S.getCurScope(), Loc, tok::star, TE);
+  }
+  if (Deref.isUsable() &&
+      Deref.get()->isModifiableLvalue(S.Context, &Loc) == Expr::MLV_Valid &&
+      !E->getType()->isObjCObjectPointerType()) {
+    S.Diag(Loc, diag::note_typecheck_add_deref_star_not_modifiable_lvalue)
----------------
Sirraide wrote:

```suggestion
    S.Diag(E->getBeginLoc(), diag::note_typecheck_add_deref_star_not_modifiable_lvalue)
```
Just checked this out locally, and currently, we’re printing e.g.
```
/tmp/test.cc:1:30: note: add '*' to dereference it
    1 | void f(int* const sasasas) { sasasas = 23; }
      |                              ~~~~~~~ ^
      |                              *
```
The caret looks a bit misplaced there because we’re using the location of the assignment for this (which makes sense for the actual error), but for this note, we should probably juse use the location of the lvalue. Changing this to `E->getBeginLoc()` gives:
```
/tmp/test.cc:1:30: note: add '*' to dereference it
    1 | void f(int* const sasasas) { sasasas = 23; }
      |                              ^~~~~~~
      |                              *
```
which I think looks a bit nicer personally.

https://github.com/llvm/llvm-project/pull/94159


More information about the cfe-commits mailing list