ZedThree / clang-tidy-review

Create a pull request review based on clang-tidy warnings
MIT License
90 stars 45 forks source link

Escape filter list in a way that can be copied and pasted into a shell with `NOMATCH` #97

Closed bwrsandman closed 1 year ago

ZedThree commented 1 year ago

Please could you give an example of before and after? I'm not quite sure what the issue is!

bwrsandman commented 1 year ago

Currently it will print this

clang-tidy-15 -p=cmake-build-presets/ninja-multi-vcpkg --config-file=".clang-tidy" -line-filter=["{'name': 'src/Game.cpp', 'lines': [[677, 677], [712, 712]]}"] "src/Game.cpp" --export-fixes=clang_tidy_review.yaml/mnt 

This causes issues with interpreters with NOMATCH such as mac's bash and zsh on linux which both complain, presumably about the [ character which can be used to match patterns.

zsh: no matches found: -line-filter=[{'name': 'src/Game.cpp', 'lines': [[677, 677], [712, 712]]}]

Escaping line filter properly gives a slightly uglier command but you can the use it in your script.

clang-tidy-15 -p=cmake-build-presets/ninja-multi-vcpkg --config-file=.clang-tidy '-line-filter=[{'"'"'name'"'"': '"'"'src/Game.cpp'"'"', '"'"'lines'"'"': [[677, 677], [712, 712]]}]' src/Game.cpp --export-fixes=clang_tidy_review.yaml/mnt

The uglyness is because of the way it escapes quotes:

...'... -> '...'"'"'...'

Is just the concatenation of '...' + "'" + '...'. It closes the string with ' quotes, open a string with " quotes in order to use the literal ', closes it and then opens another ' string.

bwrsandman commented 1 year ago

Still need to test this version of the fix

bwrsandman commented 1 year ago

I've refined the fix. The issue was a string conversion of the dict of each line change entry was introducing a quotes ["{}", "{}"] where they shouldn't be.

Not only is that an invalid json list of json objects (being instead a json list of strings), but it leaves the [...] free of quotes which can be interpreted by interpreters with NOMATCH as a regex pattern.

Using shlex.quote, the quote is now in the right place "[{}, {}]" for a json. I've also improved the way the file names are joined using shlex.join so now quotes are inserted in a smart way

["src/test.cpp", "src/test directory/test.cpp"] -> "src/test.cpp 'src/test directory/test.cpp'"