[PATCH] D54943: [clang-tidy] implement const-transformation for cppcoreguidelines-const-correctness

Florin Iucha via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Jan 4 07:41:33 PST 2020


0x8000-0000 added inline comments.


================
Comment at: clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-const-correctness-values.cpp:608
+}
+
+template <typename L, typename R>
----------------
0x8000-0000 wrote:
> JonasToth wrote:
> > JonasToth wrote:
> > > 0x8000-0000 wrote:
> > > > Please insert the this test code here:
> > > > 
> > > > ```
> > > > struct IntWrapper {
> > > > 
> > > >    unsigned low;
> > > >    unsigned high;
> > > > 
> > > >    IntWrapper& operator=(unsigned value) {
> > > >       low = value & 0xffff;
> > > >       high = (value >> 16) & 0xffff;
> > > >    }
> > > > 
> > > >    template<typename Istream>
> > > >    friend Istream& operator>>(Istream& is, IntWrapper& rhs) {
> > > >       unsigned someValue = 0;       // false positive now
> > > >       if (is >> someValue) {
> > > >          rhs = someValue;
> > > >       }
> > > >       return is;
> > > >    }
> > > > };
> > > > 
> > > > unsigned TestHiddenFriend(IntMaker& im) {
> > > >    IntWrapper iw;
> > > > 
> > > >    im >> iw;
> > > > 
> > > >    return iw.low;
> > > > }
> > > > ```
> > > clang gives me no error when I add the const there. sure it does reproduce properly?
> > Here for reference: https://godbolt.org/z/DXKMYh
> Probably I wasn't clear - I suggested adding my test code at line 608, because it needs the "IntMaker" definition at line 594.
> 
> The false positive from this const check is reported on the "unsigned someValue = 0;" line inside the template extraction operator.
Oh, I got it - don't think "shift" think "overloaded extraction operator".

In my code above, you don't know what ">>" does, and it clearly takes a mutable reference as the right side.

```
const int foo;
std::cin >> foo;
```

should not compile, either :)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D54943/new/

https://reviews.llvm.org/D54943





More information about the cfe-commits mailing list