[PATCH] D21134: clang-tidy: new check readability-misplaced-array-index

Jonathan Roelofs via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 9 07:02:37 PDT 2016


jroelofs added a comment.

In https://reviews.llvm.org/D21134#509522, @danielmarjamaki wrote:

> In https://reviews.llvm.org/D21134#508524, @jroelofs wrote:
>
> > I think the replacement is wrong for something like:
> >
> >   int *arr; int offs1, offs2;
> >   offs1[arr + offs2] = 42;
> >
> >
> > which I think would give:
> >
> >   int *arr; int offs1, offs2;
> >   arr + offs2[offs1] = 42;
> >
> >
> > If the precedence of the "indexing" argument is less than that of the subscript operator, then you need to insert parens:
> >
> >   int *arr; int offs1, offs2;
> >   (arr + offs2)[offs1] = 42;
> >
>
>
> I don't think so. The matcher says:
>
>   hasRHS(ignoringParenImpCasts(
>             anyOf(stringLiteral(), declRefExpr(hasType(pointsTo(qualType()))),
>                   memberExpr(hasType(pointsTo(qualType()))
>   
>
> I think it's hard to know what the replaced code should be. In some cases it might be better with:
>
>   arr[offs1 + offs2] = 42;
>


Oh, yeah, you're right.


https://reviews.llvm.org/D21134





More information about the cfe-commits mailing list