-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[tidy] add new check bugprone-return-const-ref-from-parameter #89497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
HerrCai0907
merged 8 commits into
llvm:main
from
HerrCai0907:bugprone-return-const-ref-from-parameter
Apr 23, 2024
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
91915f6
[tidy] add new check bugprone-return-const-ref-from-parameter
HerrCai0907 401c775
fix doc
HerrCai0907 322a819
revert list
HerrCai0907 2ce9fe7
fix comment
HerrCai0907 c63a9fa
Update clang-tools-extra/docs/clang-tidy/checks/bugprone/return-const…
HerrCai0907 4a236f3
Update clang-tools-extra/docs/clang-tidy/checks/bugprone/return-const…
HerrCai0907 406a3d1
Update clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParame…
HerrCai0907 5d8110b
Update clang-tools-extra/docs/ReleaseNotes.rst
HerrCai0907 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//===--- ReturnConstRefFromParameterCheck.cpp - clang-tidy ----------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "ReturnConstRefFromParameterCheck.h" | ||
#include "../utils/Matchers.h" | ||
#include "clang/ASTMatchers/ASTMatchFinder.h" | ||
#include "clang/ASTMatchers/ASTMatchers.h" | ||
|
||
using namespace clang::ast_matchers; | ||
|
||
namespace clang::tidy::bugprone { | ||
|
||
void ReturnConstRefFromParameterCheck::registerMatchers(MatchFinder *Finder) { | ||
Finder->addMatcher( | ||
returnStmt(hasReturnValue(declRefExpr(to(parmVarDecl(hasType( | ||
hasCanonicalType(matchers::isReferenceToConst()))))))) | ||
.bind("ret"), | ||
this); | ||
} | ||
|
||
void ReturnConstRefFromParameterCheck::check( | ||
const MatchFinder::MatchResult &Result) { | ||
const auto *R = Result.Nodes.getNodeAs<ReturnStmt>("ret"); | ||
diag(R->getRetValue()->getBeginLoc(), | ||
SimplyDanny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"returning a constant reference parameter may cause a use-after-free " | ||
"when the parameter is constructed from a temporary"); | ||
} | ||
|
||
} // namespace clang::tidy::bugprone |
40 changes: 40 additions & 0 deletions
40
clang-tools-extra/clang-tidy/bugprone/ReturnConstRefFromParameterCheck.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//===--- ReturnConstRefFromParameterCheck.h - clang-tidy --------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_RETURNCONSTREFFROMPARAMETERCHECK_H | ||
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_RETURNCONSTREFFROMPARAMETERCHECK_H | ||
|
||
#include "../ClangTidyCheck.h" | ||
|
||
namespace clang::tidy::bugprone { | ||
|
||
/// Detects return statements that return constant reference parameter as | ||
/// constant reference. This may cause use-after-free errors if the caller uses | ||
/// xvalue as arguments. | ||
HerrCai0907 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// | ||
/// For the user-facing documentation see: | ||
/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/return-const-ref-from-parameter.html | ||
class ReturnConstRefFromParameterCheck : public ClangTidyCheck { | ||
public: | ||
ReturnConstRefFromParameterCheck(StringRef Name, ClangTidyContext *Context) | ||
: ClangTidyCheck(Name, Context) {} | ||
void registerMatchers(ast_matchers::MatchFinder *Finder) override; | ||
void check(const ast_matchers::MatchFinder::MatchResult &Result) override; | ||
std::optional<TraversalKind> getCheckTraversalKind() const override { | ||
// Use 'AsIs' to make sure the return type is exactly the same as the | ||
// parameter type. | ||
return TK_AsIs; | ||
} | ||
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { | ||
return LangOpts.CPlusPlus; | ||
} | ||
}; | ||
|
||
} // namespace clang::tidy::bugprone | ||
|
||
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_RETURNCONSTREFFROMPARAMETERCHECK_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...tools-extra/docs/clang-tidy/checks/bugprone/return-const-ref-from-parameter.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
.. title:: clang-tidy - bugprone-return-const-ref-from-parameter | ||
|
||
bugprone-return-const-ref-from-parameter | ||
======================================== | ||
|
||
Detects return statements that return a constant reference parameter as constant | ||
reference. This may cause use-after-free errors if the caller uses xvalue as | ||
arguments. | ||
HerrCai0907 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
In C++, constant reference parameters can accept xvalues which will be destructed | ||
after the call. When the function returns such a parameter also as constant reference, | ||
then the returned reference can be used after the object it refers to has been | ||
destroyed. | ||
|
||
Example | ||
------- | ||
|
||
.. code-block:: c++ | ||
|
||
struct S { | ||
int v; | ||
S(int); | ||
~S(); | ||
}; | ||
|
||
const S &fn(const S &a) { | ||
return a; | ||
} | ||
|
||
const S& s = fn(S{1}); | ||
s.v; // use after free |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
clang-tools-extra/test/clang-tidy/checkers/bugprone/return-const-ref-from-parameter.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// RUN: %check_clang_tidy %s bugprone-return-const-ref-from-parameter %t | ||
|
||
using T = int; | ||
using TConst = int const; | ||
using TConstRef = int const&; | ||
|
||
namespace invalid { | ||
|
||
int const &f1(int const &a) { return a; } | ||
// CHECK-MESSAGES: :[[@LINE-1]]:38: warning: returning a constant reference parameter | ||
|
||
int const &f2(T const &a) { return a; } | ||
// CHECK-MESSAGES: :[[@LINE-1]]:36: warning: returning a constant reference parameter | ||
|
||
int const &f3(TConstRef a) { return a; } | ||
// CHECK-MESSAGES: :[[@LINE-1]]:37: warning: returning a constant reference parameter | ||
|
||
int const &f4(TConst &a) { return a; } | ||
// CHECK-MESSAGES: :[[@LINE-1]]:35: warning: returning a constant reference parameter | ||
|
||
} // namespace invalid | ||
|
||
namespace valid { | ||
|
||
int const &f1(int &a) { return a; } | ||
|
||
int const &f2(int &&a) { return a; } | ||
|
||
int f1(int const &a) { return a; } | ||
|
||
} // namespace valid |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.