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

Implement a lint for RefCell<impl Copy> #13388

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

GnomedDev
Copy link

Closes #1 (!)

As the title says. Adds a lint which checks for RefCell<impl Copy> as usage can be replaced with Cell, if T is small enough (user configurable).

As this is my first clippy lint, I'm not super firm on stuff like:

  • lint group
  • lint message wording
  • the LateLintPass checks used

so let me know if I've done something wrong and I'll get it fixed up.

changelog: [refcell_copy]: Added lint to detect usage of RefCell<T> where T is Copy.

@rustbot
Copy link
Collaborator

rustbot commented Sep 12, 2024

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Centri3 (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Sep 12, 2024
Copy link
Member

@Centri3 Centri3 left a comment

Choose a reason for hiding this comment

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

Maybe we can refactor check_local into check_expr, and check_field_def into check_item? In specific, we're missing cases like: takes_t(RefCell::new(0)) and fn f(ref_cell: RefCell<u32>)

I will also say, the types module exists. I think we should move this there

return;
}

if implements_trait(cx, inner_ty, copy_def_id, &[]) {
Copy link
Member

Choose a reason for hiding this comment

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

We should check is_from_proc_macro right here

Copy link
Member

Choose a reason for hiding this comment

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

This would probably also be better as a long let chain

Copy link
Author

Choose a reason for hiding this comment

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

Do the span.from_expansion calls earlier not catch this?

Copy link
Member

Choose a reason for hiding this comment

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

No, procedural macros can use any arbitrary span which also means its input tokens. Those won't be counted as from expansion so it'll still lint.

clippy_lints/src/copy_refcell.rs Outdated Show resolved Hide resolved
clippy_lints/src/copy_refcell.rs Outdated Show resolved Hide resolved
return;
}

let field_ty = rustc_hir_analysis::lower_ty(cx.tcx, field_def.ty);
Copy link
Member

Choose a reason for hiding this comment

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

This could be type_of (maybe only possible if check_item is used instead?)

Copy link
Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

clippy_lints/src/copy_refcell.rs Show resolved Hide resolved
clippy_lints/src/copy_refcell.rs Show resolved Hide resolved
clippy_lints/src/copy_refcell.rs Outdated Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

I'll suggest some tests:

  • Public field (shouldn't lint)
  • Public function (parameters and return type)
  • Generic code/generic T (shouldn't lint)
  • Macro code
  • External code (auxiliary::proc_macros)
  • Type aliases (probably shouldn't lint)


fn check_local(&mut self, cx: &LateContext<'tcx>, local_def: &'tcx LetStmt<'tcx>) {
// Don't want to lint macro expansions or desugaring.
if local_def.span.from_expansion() || !matches!(local_def.source, LocalSource::Normal) {
Copy link
Member

Choose a reason for hiding this comment

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

from_expansion includes desugaring

Copy link
Author

Choose a reason for hiding this comment

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

Shall I remove the matches!? Or just remove the comment?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah the matches! is unnecessary

return;
}

let inner_ty = generics.type_at(0);
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we could have a check for if this is explicitly Copy and suggesting to remove the Copy bound, but that's just a bonus

Copy link
Author

Choose a reason for hiding this comment

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

Wdym "explicitly Copy", or even removing the Copy bound? I don't see why you would want to remove the Copy implementation.

Copy link
Member

Choose a reason for hiding this comment

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

struct A<T: Copy>(RefCell<T>);

This could suggest removing the Copy bound for T, which admittedly shouldn't be done on struct definitions anyway as convention. But this applies to functions too. Whether this is useful or not is up to you.

@GnomedDev
Copy link
Author

Sounds good, I'll look into the individual comments later.

Copy link
Author

@GnomedDev GnomedDev left a comment

Choose a reason for hiding this comment

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

I specifically used local definitions and field defs to avoid false lints, as taking a RefCell directly as a function parameter should be quite rare and linting could lead to a private function taking the argument from a public function having no choice but to allow/expect.

I do not know what you are talking about with "the types module", I haven't read that in the documentation.

return;
}

let inner_ty = generics.type_at(0);
Copy link
Author

Choose a reason for hiding this comment

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

Wdym "explicitly Copy", or even removing the Copy bound? I don't see why you would want to remove the Copy implementation.

return;
}

if implements_trait(cx, inner_ty, copy_def_id, &[]) {
Copy link
Author

Choose a reason for hiding this comment

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

Do the span.from_expansion calls earlier not catch this?

clippy_lints/src/copy_refcell.rs Outdated Show resolved Hide resolved
clippy_lints/src/copy_refcell.rs Outdated Show resolved Hide resolved
return;
}

let field_ty = rustc_hir_analysis::lower_ty(cx.tcx, field_def.ty);
Copy link
Author

Choose a reason for hiding this comment

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


fn check_local(&mut self, cx: &LateContext<'tcx>, local_def: &'tcx LetStmt<'tcx>) {
// Don't want to lint macro expansions or desugaring.
if local_def.span.from_expansion() || !matches!(local_def.source, LocalSource::Normal) {
Copy link
Author

Choose a reason for hiding this comment

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

Shall I remove the matches!? Or just remove the comment?

clippy_lints/src/copy_refcell.rs Show resolved Hide resolved
clippy_lints/src/copy_refcell.rs Outdated Show resolved Hide resolved
clippy_lints/src/copy_refcell.rs Outdated Show resolved Hide resolved
clippy_lints/src/copy_refcell.rs Show resolved Hide resolved
Copy link
Member

@Centri3 Centri3 left a comment

Choose a reason for hiding this comment

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

You'll still have the same FPs anyway, for example:

let a = RefCell::new(0u32);
i_take_refcell(a);

This can be combated by using predicates_of + for_each_local_use_after_expr, but that's beyond the scope of this. Typically these FPs are fine with pedantic/restriction lints

I'm fairly sure your lint still catches let a = publicly_returns_refcell_copy(). This should be a test, and also, preventing public functions returning RefCell would then require specifically hardcoding the RefCell methods.

By the types module, I mean clippy_lints::types which is a collection of these lints under one pass.

return;
}

let field_ty = rustc_hir_analysis::lower_ty(cx.tcx, field_def.ty);
Copy link
Member

Choose a reason for hiding this comment

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

return;
}

if implements_trait(cx, inner_ty, copy_def_id, &[]) {
Copy link
Member

Choose a reason for hiding this comment

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

No, procedural macros can use any arbitrary span which also means its input tokens. Those won't be counted as from expansion so it'll still lint.

return;
}

let inner_ty = generics.type_at(0);
Copy link
Member

Choose a reason for hiding this comment

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

struct A<T: Copy>(RefCell<T>);

This could suggest removing the Copy bound for T, which admittedly shouldn't be done on struct definitions anyway as convention. But this applies to functions too. Whether this is useful or not is up to you.


fn check_local(&mut self, cx: &LateContext<'tcx>, local_def: &'tcx LetStmt<'tcx>) {
// Don't want to lint macro expansions or desugaring.
if local_def.span.from_expansion() || !matches!(local_def.source, LocalSource::Normal) {
Copy link
Member

Choose a reason for hiding this comment

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

Yeah the matches! is unnecessary

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.

RefCell<T> where T: Copy and T in current crate
4 participants