Skip to content

Downgrade integer_division to restriction #4210

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

Merged
merged 1 commit into from
Jun 19, 2019
Merged

Downgrade integer_division to restriction #4210

merged 1 commit into from
Jun 19, 2019

Conversation

dtolnay
Copy link
Member

@dtolnay dtolnay commented Jun 15, 2019

I believe that this lint falls outside of the scope of opinionated pedantism of the other pedantic lints.

changelog: Downgrade integer_division lint from pedantic to restriction

@Manishearth
Copy link
Member

I feel like there needs to be more justification for this. But I'm largely okay with this.

@dtolnay
Copy link
Member Author

dtolnay commented Jun 15, 2019

The lint's justification is:

Why is this bad? When outside of some very specific algorithms, integer division is very often a mistake because it discards the remainder.

I question whether there is evidence that this is "very often" the case. Out of the number of times that integer division has been used in Rust, what fraction of them were mistakes that should have used float division instead? Less than 1%?

I have no problem with it as a restriction for someone who consciously decides they want to avoid integer division in their code.

@dtolnay
Copy link
Member Author

dtolnay commented Jun 15, 2019

error: integer division
 --> $DIR/integer_division.rs:7:13
  |
7 |     let p = a / b;
  |             ^^^^^
  |
  = help: division of integers may cause loss of precision. consider using floats.

Consider whether pushing people to use float division will fix more problems than it solves. In reality, using unnecessary float division instead of integer division is what often causes loss of precision.

fn main() {
    let numerator: u32 = 12_345_678 * 3;
    let denominator = 3;
    println!("integer quotient: {}", numerator / denominator);
    println!("float quotient:   {}", numerator as f32 / denominator as f32);
}
integer quotient: 12345678
float quotient:   12345677

The integer quotient is exactly correct while the float quotient loses precision.

This lint seems intended to help people who don't yet have a firm grasp of the difference between integer and float, but I think it will do more harm than good for that group.

@ghost
Copy link

ghost commented Jun 19, 2019

This lint will have 99% false positive rate. (It triggers on all integer division which means every instance of integer division is either a bug or a false positive.) In its current form, it's just going to annoy people. It would be great if we could move this to restriction before 1.36 is released.

@flip1995
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Jun 19, 2019

📌 Commit f88a387 has been approved by flip1995

@bors
Copy link
Contributor

bors commented Jun 19, 2019

⌛ Testing commit f88a387 with merge 97f8caa...

bors added a commit that referenced this pull request Jun 19, 2019
Downgrade integer_division to restriction

I believe that this lint falls outside of the scope of opinionated pedantism of the other pedantic lints.

changelog: Downgrade integer_division lint from pedantic to restriction
@bors
Copy link
Contributor

bors commented Jun 19, 2019

☀️ Test successful - checks-travis, status-appveyor
Approved by: flip1995
Pushing 97f8caa to master...

@bors bors merged commit f88a387 into rust-lang:master Jun 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants