[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);
+  }
----------------
zygoloid wrote:

It's not really feasible to use a `TentativeAnalysisScope` for this. Here are three reasons:

1) If `ActOnUnaryOp` produces an "immediate context" diagnostic (the common case), such as a warning, then the note you produce below will be attached to that diagnostic instead of to the original diagnostic, so your note will be suppressed.
2) If `ActOnUnaryOp` triggers template instantiation, then `TentativeAnalysisScope` will not suppress diagnostics outside of the immediate context, potentially leading to additional spurious diagnostics being produced.
3) If this function is used to attach a note to a warning / extension diagnostic rather than an error (which doesn't appear to be the case right now, but it seems plausible that someone would make such a change to `CheckForModifiableLValue` in the future), then the diagnostics produced by (2) would lead to rejecting a valid program.

Checking for an overloaded `operator*` seems overly complicated here, so I think the right thing to test here is just whether the expression has pointer type, and the pointee type is an object type (not void or a function type).

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


More information about the cfe-commits mailing list