Skip to content
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

Warn on expressions that contain logical operators but no braces #313

Closed
m-o-e opened this issue Nov 30, 2022 · 3 comments · Fixed by #510
Closed

Warn on expressions that contain logical operators but no braces #313

m-o-e opened this issue Nov 30, 2022 · 3 comments · Fixed by #510

Comments

@m-o-e
Copy link

m-o-e commented Nov 30, 2022

A pretty mean (and probably common) trap:

$ cat >test.cr <<EOC
foo = ["batz"]

if foo.includes? "bar" || foo.includes? "batz"
  puts "this code is bug-free"
end
EOC
$ crystal test.cr
(no output)
$ ameba test.cr

Inspecting 1 file

.

Finished in 2.21 milliseconds
1 inspected, 0 failures

Can easily go unnoticed even with test-coverage.
I think it would be great if ameba would warn about it.

@straight-shoota
Copy link
Contributor

straight-shoota commented Nov 23, 2024

The proposed implementation in #510 catches a lot of issues which could be considered false positives and be more of a nuisance than helpful.

For example, the following expressions seem very clear without parantheses. Yet this rule complains about them.

puts message || "no message"

printf "%s, %d\n", message || "no message", 1

Part of the reason is of course that puts and printf are well-known methods which return only Nil. So it wouldn't make sense to "or" their return values.
But in the second call there is simply no possibility for misinterpretation because there is another argument after the binary op.

I wouldn't consider it completely inadequate to require adding parentheses in these cases. But I'm not convinced it should be necessary to do so.

The example from the OP of course would certainly benefit from a complaint about missing parenthesis.
Maybe we just need a more fine-grained set of conditions? I'm not sure if there's any feasible way to discern this though.

@nobodywasishere
Copy link
Contributor

That's valid, thank you for the feedback. Maybe it can be narrowed down to a Call for each args where if any arg is BinaryOp, and the right of that is another Call with a BinaryOp. A little specific, but maybe that'd be fine?

@Sija
Copy link
Member

Sija commented Nov 24, 2024

I agree with @straight-shoota. We can start with a narrow scope and expand it later if needed.

@Sija Sija changed the title [Feature Request] Warn on expressions that contain logical operators but no braces Warn on expressions that contain logical operators but no braces Nov 25, 2024
@Sija Sija added this to the 1.7.0 milestone Nov 25, 2024
@Sija Sija closed this as completed in #510 Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants