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

unnecessary call to max function #11901

Closed
FelixMaetzler opened this issue Dec 1, 2023 · 13 comments · Fixed by #12368
Closed

unnecessary call to max function #11901

FelixMaetzler opened this issue Dec 1, 2023 · 13 comments · Fixed by #12368
Assignees
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy

Comments

@FelixMaetzler
Copy link

FelixMaetzler commented Dec 1, 2023

What it does

In todays AdventOfCode i caught myself using the below example.
The max() call of an unsigned number with 0 is unnecessary.
It would be nice if clippy would say something about that.

the maximum between 0 and an unsigned number is always the number.

Please let me know if i did anything wrong.
Have a nice day.

Advantage

  • Remove of unnecessary function call
  • Reduction of Code Complexity

Drawbacks

No response

Example

let x: usize = 5;
let y: usize = 8;
let ret = 0.max(x.saturating_sub(y));

Could be written as:

let x: usize = 5;
let y: usize = 8;
let ret = x.saturating_sub(y);
@FelixMaetzler FelixMaetzler added the A-lint Area: New lints label Dec 1, 2023
@Alexendoo Alexendoo added the good-first-issue These issues are a good way to get started with Clippy label Dec 1, 2023
@kavania2002
Copy link

I would like to contribute in this issue if you don't mind.
Though I'll take some time as I'm new to this community but I am highly interested.

@FelixMaetzler
Copy link
Author

FelixMaetzler commented Dec 2, 2023

Yeah feel free to do so.
I’m new to this community and “public” GitHub too.
I would love to see with what you come up with.

@FelixMaetzler
Copy link
Author

I pushed the wrong button and I don’t know how to fix it.
Sorry

@FelixMaetzler FelixMaetzler reopened this Dec 2, 2023
@kavania2002
Copy link

@rustbot claim

@kavania2002
Copy link

@FelixMaetzler so you meant to say that when you run the lint on

let x: usize = 5;
let y: usize = 8;
let ret = x.saturating_sub(y);

it changes to

let x: usize = 5;
let y: usize = 8;
let ret = 0.max(x.saturating_sub(y));

Am I going in the right direction?

@kavania2002
Copy link

Or do you mean that when someone runs cargo clippy on

let x: usize = 5;
let y: usize = 8;
let ret = 0.max(x.saturating_sub(y));

should give an error/warning?

@y21
Copy link
Member

y21 commented Dec 3, 2023

It should probably warn on 0.max(<anything>) yes. One can just use the argument directly. 0.max(x + y) is the same as x + y for unsigned integer types

@FelixMaetzler
Copy link
Author

Or do you mean that when someone runs cargo clippy on

let x: usize = 5;
let y: usize = 8;
let ret = 0.max(x.saturating_sub(y));

should give an error/warning?

yes exactly that.

like @y21 said, you could generalize that to 0.max(<anything>) can be replaced by <anything> if <anything> evaluates (or is) an unsigned integer.
the part with the saturating_sub() was just my specific case, where i discovered that you don't need the max() method.

In my opinion this should give a similar/same cargo clippy output like following:

let b = 12 + 0;
dbg!(b);

@kavania2002
Copy link

Gotcha!

@lightlessdays
Copy link

@rustbot claim

@rustbot rustbot assigned lightlessdays and unassigned kavania2002 Dec 8, 2023
@vohoanglong0107
Copy link
Contributor

vohoanglong0107 commented Feb 23, 2024

Hi @lightlessdays, are you still working on this? Would you mind if I claim it?

@lightlessdays
Copy link

lightlessdays commented Feb 24, 2024

Hi @lightlessdays, are you still working on this? Would you mind if I claim it?

Hey, I am sorry, I got busy with my university exams. Well... You can claim it I suppose. Really sorry about ghosting this issue. @vohoanglong0107

@vohoanglong0107
Copy link
Contributor

Thanks @lightlessdays. Then I will start working on this

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants