================ @@ -42,10 +43,28 @@ void AvoidReturnWithVoidValueCheck::check( const auto *VoidReturn = Result.Nodes.getNodeAs<ReturnStmt>("void_return"); if (IgnoreMacros && VoidReturn->getBeginLoc().isMacroID()) return; - if (!StrictMode && !Result.Nodes.getNodeAs<CompoundStmt>("compound_parent")) + const auto *SurroundingBlock = + Result.Nodes.getNodeAs<CompoundStmt>("compound_parent"); + if (!StrictMode && !SurroundingBlock) return; + const StringRef ReturnExpr = + Lexer::getSourceText(CharSourceRange::getTokenRange( + VoidReturn->getRetValue()->getSourceRange()), + *Result.SourceManager, getLangOpts()); + SourceLocation SemicolonPos; + if (const std::optional<Token> NextToken = + Lexer::findNextToken(VoidReturn->getRetValue()->getEndLoc(), + *Result.SourceManager, getLangOpts())) + SemicolonPos = NextToken->getEndLoc(); + std::string Replacement = (ReturnExpr + "; return;").str(); + if (!SurroundingBlock) + Replacement = "{" + Replacement + "}"; diag(VoidReturn->getBeginLoc(), "return statement within a void function " - "should not have a specified return value"); + "should not have a specified return value") + << FixItHint::CreateReplacement( + CharSourceRange::getTokenRange(VoidReturn->getBeginLoc(), + SemicolonPos), ---------------- HerrCai0907 wrote:
what will happen if `SemicolonPos` is invalid? https://github.com/llvm/llvm-project/pull/81420 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits