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

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 17 12:33:42 PDT 2024


================
@@ -13273,6 +13273,23 @@ 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(E->getBeginLoc(),
+           diag::note_typecheck_add_deref_star_not_modifiable_lvalue)
+        << E->getSourceRange()
+        << FixItHint::CreateInsertion(E->getBeginLoc(), "*");
----------------
zygoloid wrote:

We shouldn't be suggesting adding a `*` unless that would actually make sense in context. Eg, given:
```c++
const int *const p;
const int *const q;
p = q;
```
... we should not suggest a dereference -- the problem is clearly that `p` is `const`, and adding a `*` doesn't help.

I suspect we simply don't have enough information to do this from within `CheckForModifiableLvalue`.

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


More information about the cfe-commits mailing list