[PATCH] D15332: new clang-tidy checker readability-non-const-parameter

Daniel Marjamäki via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 8 02:20:42 PST 2016


danielmarjamaki added inline comments.

================
Comment at: test/clang-tidy/readability-non-const-parameter.cpp:3
@@ +2,3 @@
+
+// Currently the checker only warns about pointer arguments.
+//
----------------
LegalizeAdulthood wrote:
> danielmarjamaki wrote:
> > LegalizeAdulthood wrote:
> > > How hard is it to extend it to references?
> > > 
> > > Certainly the confusion about what is const is easier to resolve in the case of references because the references themselves are immutable.
> > If a "int &" reference parameter is not written then probably it's better to pass it by value than making it const. I would prefer that unless it has to use "int &" to comply with some interface.
> > 
> > I realize that the same can be said about pointers. If there is a "int *p" and you just read the value that p points at .. maybe sometimes it would be preferable to pass it by value.
> I thought the point of this check was to identify stuff passed by reference/pointer that could instead be passed by const reference/pointer.
> 
> Passing a read-only number type (`short`, `char`, `int`, `float`, `double`, etc.) parameter by pointer or by reference/pointer is a code smell, but this check isn't trying to identify that situation.
> 
> I'm more interested in things like:
> 
> ```
> void foo(std::string &s);
> ```
> 
> becoming
> 
> ```
> void foo(const std::string &s);
> ```
> 
> when `s` is treated as a read-only value within the implementation of `foo`.
I agree pointers and references are related from user perspective.

But technically I think we can't reuse the same markCanNotBeConst code.

This expression "p += 2" does not prevent that *p is const. But "r += 2" would prevent that the reference r is const.

> I'm more interested in things like:

Yes that would be interesting. However I only warn about number types right now. As my last example in the rst file shows I don't warn for non-constant struct pointers for a reason.

Imho, I would like to have a warning when the data is modified even though *p is const. Like this:

  struct S { int *a; int *b; };
  int f3(const struct S * const p) {
    *(p->a) = 0; // <- I would like to have warning here
  }

Also dealing with C++ is not my priority. I write this check so I can use it on C code.



http://reviews.llvm.org/D15332





More information about the cfe-commits mailing list