[clang] [clang] Fix-it hint for `++this` -> `++*this` when deref is modifiable (PR #94159)
Rajveer Singh Bharadwaj via cfe-commits
cfe-commits at lists.llvm.org
Thu Jun 6 07:06:51 PDT 2024
================
@@ -13587,10 +13602,47 @@ static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) {
SourceRange Assign;
if (Loc != OrigLoc)
Assign = SourceRange(OrigLoc, OrigLoc);
- if (NeedType)
+ if (NeedType) {
S.Diag(Loc, DiagID) << E->getType() << E->getSourceRange() << Assign;
- else
+ } else {
+ ExprResult Deref;
+ {
+ Sema::TentativeAnalysisScope Trap(S);
+ Deref = S.ActOnUnaryOp(S.getCurScope(), Loc, tok::star, E);
+ }
S.Diag(Loc, DiagID) << E->getSourceRange() << Assign;
+ if (Deref.isUsable() &&
+ Deref.get()->isModifiableLvalue(S.Context, &Loc) == Expr::MLV_Valid) {
+ std::string exprType;
+ switch (E->getStmtClass()) {
+ case Stmt::CStyleCastExprClass: {
+ const auto *CSC = cast<CStyleCastExpr>(E);
+ exprType = CSC->getType().getAsString();
+ break;
+ }
+ case Stmt::CXXConstCastExprClass: {
+ const auto *CXXCCE = cast<CXXConstCastExpr>(E);
+ exprType = CXXCCE->getTypeAsWritten().getAsString();
+ break;
+ }
+ case Stmt::CXXReinterpretCastExprClass: {
+ const auto *CXXRCE = cast<CXXReinterpretCastExpr>(E);
+ exprType = CXXRCE->getTypeAsWritten().getAsString();
+ break;
+ }
+ case Stmt::CXXThisExprClass: {
+ exprType = "this";
+ break;
+ }
+ default: {
+ }
+ }
+ S.Diag(Loc, diag::note_typecheck_expression_not_modifiable_lvalue)
+ << E->getSourceRange() << Assign
+ << FixItHint::CreateInsertion(E->getBeginLoc(),
+ "*(" + exprType + ")");
+ }
----------------
Rajveer100 wrote:
Ahh, I thought of making it more descriptive (like Rust), you want me to remove this and just suggest `*(...)`?
https://github.com/llvm/llvm-project/pull/94159
More information about the cfe-commits
mailing list