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

via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 7 08:06:25 PDT 2024


================
@@ -13367,6 +13367,20 @@ static void DiagnoseConstAssignment(Sema &S, const Expr *E,
         if (!DiagnosticEmitted) {
           S.Diag(Loc, diag::err_typecheck_assign_const)
               << ExprRange << ConstVariable << VD << VD->getType();
+          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_expression_not_modifiable_lvalue)
+                << E->getSourceRange()
+                << FixItHint::CreateInsertion(E->getBeginLoc(), "*");
+          }
----------------
Sirraide wrote:

Can you maybe move this into a helper function, because from what I can tell at least, this is (almost) the exact same code as in `CheckForModifiableLValue`. The API could be something along the lines of this
```c++
static void MaybeSuggestDerefFixIt(Sema& S, Expr* E) { ... }
```
though it might potentially need a few more parameters if there are differences between this here and in `CheckForModifiableLValue` that I’m missing.

(The main error diagnostic, e.g. the `diag::note_typecheck_assign_const` that’s emitted below here, should *not* be moved into the helper imo.)

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


More information about the cfe-commits mailing list