Skip to content

[NFC][CLANG] Fix static analyzer bugs about unnecessary object copies with auto keyword #85962

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
merged 1 commit into from
Mar 26, 2024

Conversation

smanna12
Copy link
Contributor

@smanna12 smanna12 commented Mar 20, 2024

Reported by Static Analyzer Tool:
In clang::dataflow::Environment::initialize(): Using the auto keyword without an & causes the copy of an object of type LambdaCapture

… with auto keyword

  Reported by Static Analyzer Tool:

  In clang::dataflow::Environment::initialize(): Using the auto keyword without an & causes the copy of an object of type LambdaCapture
@smanna12 smanna12 requested a review from tahonermann March 20, 2024 16:42
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:dataflow Clang Dataflow Analysis framework - https://clang.llvm.org/docs/DataFlowAnalysisIntro.html clang:analysis labels Mar 20, 2024
@llvmbot
Copy link
Member

llvmbot commented Mar 20, 2024

@llvm/pr-subscribers-clang-analysis

@llvm/pr-subscribers-clang

Author: None (smanna12)

Changes

Reported by Static Analyzer Tool:
In clang::dataflow::Environment::initialize(): Using the auto keyword without an & causes the copy of an object of type LambdaCapture


Full diff: https://github.com/llvm/llvm-project/pull/85962.diff

1 Files Affected:

  • (modified) clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp (+1-1)
diff --git a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
index cc1ebd511191a9..70e0623805a8cf 100644
--- a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
+++ b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
@@ -416,7 +416,7 @@ void Environment::initialize() {
     assert(Parent != nullptr);
 
     if (Parent->isLambda()) {
-      for (auto Capture : Parent->captures()) {
+      for (const auto &Capture : Parent->captures()) {
         if (Capture.capturesVariable()) {
           const auto *VarDecl = Capture.getCapturedVar();
           assert(VarDecl != nullptr);

@smanna12 smanna12 changed the title [NFC][CLANG] Fix static analyzer bugs about unnecessary object copies… [NFC][CLANG] Fix static analyzer bugs about unnecessary object copies with auto keyword Mar 20, 2024
@ymand
Copy link
Collaborator

ymand commented Mar 20, 2024

Looks good, but per LLVM style guidelines, should we go ahead and replace the auto as well?

Copy link
Contributor

@tahonermann tahonermann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks fine to me. LambdaCapture consists of a pointer and two source locations so is relatively small and thus, this doesn't address a performance concern. Having the variable be const is good regardless to avoid unintended mutation.

@smanna12
Copy link
Contributor Author

Thank you for reviews!

@smanna12 smanna12 merged commit 4c4ea24 into llvm:main Mar 26, 2024
@smanna12 smanna12 deleted the FixAutoBugs branch March 26, 2024 17:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:analysis clang:dataflow Clang Dataflow Analysis framework - https://clang.llvm.org/docs/DataFlowAnalysisIntro.html clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants