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

via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 6 07:07:47 PDT 2024


================
@@ -13367,6 +13367,21 @@ 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(),
+                                              "*" + VD->getNameAsString());
----------------
Sirraide wrote:

Er, I candidly don’t follow why we would want to do any replacement here instead of just inserting `*`?

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


More information about the cfe-commits mailing list