New user self-registration is disabled due to spam. For an account please email bugs-admin@lists.llvm.org with your e-mail address and full name.

Bug 44746 - False positive for cppcoreguidelines-init-variables in range based for loop in template function
Summary: False positive for cppcoreguidelines-init-variables in range based for loop i...
Status: RESOLVED FIXED
Alias: None
Product: clang-tools-extra
Classification: Unclassified
Component: clang-tidy (show other bugs)
Version: unspecified
Hardware: PC Linux
: P normal
Assignee: Unassigned Clang Bugs
URL: https://godbolt.org/z/s_bCSi
Keywords:
Depends on:
Blocks: release-10.0.0
  Show dependency tree
 
Reported: 2020-01-31 13:37 PST by Peter Gerell
Modified: 2020-02-10 05:20 PST (History)
6 users (show)

See Also:
Fixed By Commit(s): efcd09cea9a51c522954aa24e4b5513266daf6c3


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Peter Gerell 2020-01-31 13:37:32 PST
clang-tidy-10 as of 2020-01-31 gives the following false positive

====== test.cpp ======
template<typename RANGE>
void f(RANGE r) {
  for (char c : r) {
      
  }
}
======================

$ clang-tidy test.cpp --checks='cppcoreguidelines*'
test.cpp:3:13: warning: variable 'c' is not initialized [cppcoreguidelines-init-variables]
  for (char c : r) {
            ^
                = 0

Also reproducible on trunk:
https://godbolt.org/z/s_bCSi
Comment 1 Nathan James 2020-02-01 15:25:10 PST
This is an odd case, for some reason this code doesn't trigger the warning

==========================
template <typename T>
void g(std::vector<T> r) {
  for (T c : r) {
  }
}
==========================

https://godbolt.org/z/L9NdQx

Anyway here is a fix, just disable the check when we detect the var decl is the loop variable of a for range statement.

https://reviews.llvm.org/D73843
Comment 3 Peter Gerell 2020-02-07 02:13:59 PST
Thank you.
I have verified that this solves my original problem.
Will this be cherry-picked to the 10.x branch?
Comment 4 Nathan James 2020-02-07 03:19:18 PST
I'll add it as a release blocker but I'm not entirely sure it should go in there. I'll let Hans have the final verdict on this matter
Comment 5 Hans Wennborg 2020-02-10 05:20:55 PST
(In reply to Nathan James from comment #4)
> I'll add it as a release blocker but I'm not entirely sure it should go in
> there. I'll let Hans have the final verdict on this matter

Sounds good to me. Cherry-picked as 84cda4cceabdfec4f130bfafe7bbd050aa65b2ec