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

trigger obfuscated_if_else for .then(..).unwrap_or(..) #14021

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lapla-cogito
Copy link
Contributor

@lapla-cogito lapla-cogito commented Jan 18, 2025

part of #9100

The obfuscated_if_else lint currently only triggers for the pattern .then_some(..).unwrap_or(..), but there're other cases where this lint should be triggered, one of which is .then(..).unwrap_or(..).

changelog: [obfuscated_if_else]: trigger lint for the .then(..).unwrap_or(..) pattern as well

@rustbot
Copy link
Collaborator

rustbot commented Jan 18, 2025

r? @llogiq

rustbot has assigned @llogiq.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jan 18, 2025
@lapla-cogito lapla-cogito force-pushed the obfusifelse_then_unwrapor branch from 0fb2747 to 4eb060d Compare January 18, 2025 10:14

// Remove the enclosing parentheses if they exist
// e.g. "(a == 1)" -> "a == 1"
fn remove_needless_parentheses(s: &str) -> String {
Copy link
Contributor Author

@lapla-cogito lapla-cogito Jan 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the first implementation, I noticed that some suggestions may contain needless parentheses (see this lintcheck run). Hence I implemented this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you work with Sugg (from clippy_utils/src/sugg.rs) which already manipulates parentheses, instead of string snippets, starting from HIR nodes?

@lapla-cogito lapla-cogito force-pushed the obfusifelse_then_unwrapor branch 2 times, most recently from 55f96f9 to 52e688c Compare January 18, 2025 12:26
@lapla-cogito lapla-cogito force-pushed the obfusifelse_then_unwrapor branch from 52e688c to ef52d42 Compare January 18, 2025 12:27
@lapla-cogito
Copy link
Contributor Author

lapla-cogito commented Jan 18, 2025

Also I'm willing to implement for other patterns that should be covered by obfuscated_if_else. (But in other PRs)


// Remove the enclosing parentheses if they exist
// e.g. "(a == 1)" -> "a == 1"
fn remove_needless_parentheses(s: &str) -> String {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you work with Sugg (from clippy_utils/src/sugg.rs) which already manipulates parentheses, instead of string snippets, starting from HIR nodes?

fn remove_needless_parentheses(s: &str) -> String {
if has_enclosing_paren(s) {
s[1..s.len() - 1].to_string()
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if you didn't use Sugg, you could return a &str instead of allocating a String

--> tests/ui/obfuscated_if_else.rs:9:5
|
LL | (a == 1).then_some("a").unwrap_or("b");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `if a == 1 { "a" } else { "b" }`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should check for side effects, as the arguments to then_some() and unwrap_or() will always be evaluated, while the one in then and else clauses will not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants