rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

unnecessary call to max function

Open FelixMaetzler opened this issue 1 year ago • 13 comments

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 avatar Dec 01 '23 12:12 FelixMaetzler

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.

kavania2002 avatar Dec 02 '23 17:12 kavania2002

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 avatar Dec 02 '23 18:12 FelixMaetzler

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

FelixMaetzler avatar Dec 02 '23 18:12 FelixMaetzler

@rustbot claim

kavania2002 avatar Dec 03 '23 08:12 kavania2002

@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 avatar Dec 03 '23 09:12 kavania2002

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?

kavania2002 avatar Dec 03 '23 09:12 kavania2002

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

y21 avatar Dec 03 '23 09:12 y21

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);

FelixMaetzler avatar Dec 03 '23 10:12 FelixMaetzler

Gotcha!

kavania2002 avatar Dec 03 '23 10:12 kavania2002

@rustbot claim

lightlessdays avatar Dec 08 '23 08:12 lightlessdays

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

vohoanglong0107 avatar Feb 23 '24 13:02 vohoanglong0107

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

lightlessdays avatar Feb 24 '24 08:02 lightlessdays

Thanks @lightlessdays. Then I will start working on this

@rustbot claim

vohoanglong0107 avatar Feb 26 '24 03:02 vohoanglong0107