-
Notifications
You must be signed in to change notification settings - Fork 566
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
python rules do not match list comprehension correctly #4221
Comments
This issue is synced in Linear at https://linear.app/r2c/issue/PA-664/python-rules-do-not-match-list-comprehension-correctly. |
Updated version of rule to demonstrate where we first encountered this: https://semgrep.dev/s/5D50 We have a method |
More updates: I'm unclear if this is the same issue but it seems similar even though it's not actually a list comprehension. A method that ends with a long compound boolean expression was not matched correctly: https://semgrep.dev/s/66G1 |
hmm you see @emjin with this last example I am more and more tempted to use ParenExpr there too |
This closes #4221 test plan: We know have the correct range for the example below. the end part used to be col 34, forgetting the trailing ']' in the comprehension and now it ends at col 35! ``` semgrep-core -lang py -f tests/python/misc_comprehension.sgrep tests/python/misc_comprehension.py -json | jq ... col = 35 ```
Thanks for the report @rgraber. I expect this fix will go out in 0.77 next week! |
Describe the bug
Using a python rule to match a list comprehension fails, in certain cases, to correctly match the full expression, resulting in extra
]
s appearing after fixes are applied. Also, possibly a separate bug, repeated metavariables don't seem to work inside a list comprehension.To Reproduce
https://semgrep.dev/s/0ykw
Expected behavior
$Z = [ $X for $X in $Y]
(python-replace-useless-comprehension-misses) to matchthing = [ x for x in range(10) ]
thing = [ x for x in range(10)]
withthing = range(10)
instead ofthing = range(10)]
Screenshots
Worth nothing: it looks like the second matcher is matching everything except the last
]
, which is odd, but explains why the replacement has an extra]
at the endWhat is the priority of the bug to you?
Environment
If not using semgrep.dev: are you running off docker, an official binary, a local build?
The text was updated successfully, but these errors were encountered: