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

Add new lint: rc_mutex #7316

Merged
merged 8 commits into from
Jul 3, 2021
Merged

Add new lint: rc_mutex #7316

merged 8 commits into from
Jul 3, 2021

Conversation

lengyijun
Copy link
Contributor

@lengyijun lengyijun commented Jun 4, 2021

changelog: Add new lint rc_mutex.

It lints on Rc<Mutex<T>>.

Rc<Mutex<T>> should be corrected to Rc<RefCell<T>>

@rust-highfive
Copy link

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @llogiq (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jun 4, 2021
@lengyijun lengyijun changed the title Add new line: rc_mutex Add new lint: rc_mutex Jun 4, 2021
Copy link
Contributor

@llogiq llogiq left a comment

Choose a reason for hiding this comment

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

This should work well enough as a restriction lint. However, I think the suggestion is incomplete and the tests should be fleshed out more.

declare_clippy_lint! {
/// **What it does:** Checks for `Rc<Mutex<T>>`.
///
/// **Why is this bad?** `Rc<Mutex<T>>` may introduce a deadlock in single thread. Consider
Copy link
Contributor

Choose a reason for hiding this comment

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

This would also apply to Arc<Mutex<T>>. The distinction is that Rc is not thread-safe, and thus the Mutex can never be shared.

This may also mean that Arc<Mutex<T>> was intended, so suggesting Rc<RefCell<T>> would go into the wrong direction.

if let TyKind::Path(ref qpath_inner)=ty.kind;

then{
let mut applicability = Applicability::MachineApplicable;
Copy link
Contributor

Choose a reason for hiding this comment

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

Unless RefCell has the same or a superset of Mutex's interface, applying this automatically can lead to compiler errors. Therefore we should not make this auto-applicable.

Worse, it's never sufficient to just change one type.

cx,
RC_MUTEX,
hir_ty.span,
"you seem to be trying to use `Rc<Mutex<T>>`. Consider using `Rc<RefCell<T>>`",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"you seem to be trying to use `Rc<Mutex<T>>`. Consider using `Rc<RefCell<T>>`",
"found `Rc<Mutex<_>>`. Consider using `Rc<RefCell<_>>` instead",

Two,
}

pub fn test1<T>(foo: Rc<Mutex<T>>) {}
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd like to see a test that actually creates an instance.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could you please check if I added correct test in my latest commit?
I'm not sure I did it right.


pub fn test3(foo: Rc<Mutex<SubT<usize>>>) {}

fn main() {}
Copy link
Contributor

Choose a reason for hiding this comment

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

How about calling test1, test2, etc. from here?

This is something the lint might want to consider, btw., and also the reason not to apply it automatically.

@flip1995
Copy link
Member

flip1995 commented Jun 8, 2021

Is there an issue open for this lint? Or was it discussed in any other place beforehand? If so, please link to it in the PR description.

@lengyijun
Copy link
Contributor Author

There is no related issue and discussion.

/// **Why is this bad?** `Rc` is used in single thread and `Mutex` is used in multi thread.
/// Consider using `Rc<RefCell<T>>` in single thread or `Arc<Mutex<T>>` in multi thread.
///
/// **Known problems:** None.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think there may be instances where combining generics and traits could require this actual type combination, and following the suggestion would make it impossible to implement the traits.

We should consider those cases false positives and make a note of a possible problem. I don't think this problem is worrisome for the lint (it's unlikely to happen in practise), but we should note it nonetheless.

@llogiq
Copy link
Contributor

llogiq commented Jun 19, 2021

Sorry for letting you wait for so long. I had some $life keeping me busy.

I'd like a more fleshed out explanation of the possible false positives. How about: "Sometimes combining generic types can lead to the requirement that a type use Rc in conjunction with Mutex. We must consider those cases false positives, but alas they are quite hard to rule out. Luckily they are also rare."

@llogiq
Copy link
Contributor

llogiq commented Jul 3, 2021

Thank you! @bors r+

@bors
Copy link
Contributor

bors commented Jul 3, 2021

📌 Commit b9cc2fe has been approved by llogiq

@bors
Copy link
Contributor

bors commented Jul 3, 2021

⌛ Testing commit b9cc2fe with merge 8420999...

@bors
Copy link
Contributor

bors commented Jul 3, 2021

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: llogiq
Pushing 8420999 to master...

@bors bors merged commit 8420999 into rust-lang:master Jul 3, 2021
@lengyijun lengyijun deleted the rc_mutex branch October 27, 2023 10:41
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.

5 participants