Skip to content

mutex_integer suggests using AtomicIsize over Mutex<i128> #9872

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

Closed
chorman0773 opened this issue Nov 18, 2022 · 2 comments · Fixed by #12008
Closed

mutex_integer suggests using AtomicIsize over Mutex<i128> #9872

chorman0773 opened this issue Nov 18, 2022 · 2 comments · Fixed by #12008
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@chorman0773
Copy link

chorman0773 commented Nov 18, 2022

Summary

mutex_integer suggests using AtomicIsize instead of Mutex, including when I store 1<<64 to it.

The value of course is not representable as isize, so will cause a panic at runtime if replaced (or require an as cast that truncates).

Lint Name

mutex_integer

Reproducer

I tried this code:

#![deny(clippy::nursery)]
use std::sync::Mutex;
let m = Mutex::<i128>::new(0);

std::thread::scope(|s|{
     s.spawn(|| *m.lock().unwrap() = 1<<64);
})

I saw this happen:

error: consider using an `AtomicIsize` instead of a `Mutex` here; if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
 --> src/main.rs:4:9
  |
4 | let m = Mutex::<i128>::new(0);
  |         ^^^^^^^^^^^^^^^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#mutex_integer
note: the lint level is defined here
 --> src/main.rs:1:54
  |
1 | #![allow(dead_code, clippy::let_unit_value)] #![deny(clippy::nursery)]
  |                                                      ^^^^^^^^^^^^^^^
  = note: `#[deny(clippy::mutex_integer)]` implied by `#[deny(clippy::nursery)]`

error: consider using an `AtomicIsize` instead of a `Mutex` here; if you just want the locking behavior and not the internal type, consider using `Mutex<()>`
 --> src/main.rs:7:18
  |
7 |      s.spawn(|| *m.lock().unwrap() = 1<<64);
  |                  ^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#mutex_integer

error: could not compile `playground` due to 2 previous errors

I expected to see this happen:

No diagnostic.

Version

Nightly on play.rust-lang.org 2022-11-17 (via Ferris bot ?clippy command on rust community discord)

Additional Labels

No response

@chorman0773 chorman0773 added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Nov 18, 2022
@chorman0773
Copy link
Author

Is there a label for "Suggestion causes logic error"? I-suggestion-causes-error seems to imply ill-formed program.

@J-ZhengLi
Copy link
Member

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants