Skip to content

Conversation

EFanZh
Copy link

@EFanZh EFanZh commented Apr 20, 2025

changelog: [global_variables]: check static variables with interior mutability.

@rustbot
Copy link
Collaborator

rustbot commented Apr 20, 2025

r? @Jarcho

rustbot has assigned @Jarcho.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Apr 20, 2025
@EFanZh EFanZh force-pushed the add-global-variables-lint branch from e1e98ac to 2c457fd Compare April 20, 2025 10:20
@Jarcho
Copy link
Contributor

Jarcho commented Apr 20, 2025

This looks like it's the same lint as declare_interior_mutable_const but for static items. For consistency this should be named declare_interior_mutable_statics (plural, the other one violates the naming guide) and should share the same implementation.

@EFanZh EFanZh force-pushed the add-global-variables-lint branch from 3ce5790 to 75b29dd Compare April 26, 2025 07:11
@EFanZh
Copy link
Author

EFanZh commented Apr 26, 2025

@Jarcho: I have moved the implementation together with declare_interior_mutable_const, but I didn’t change the name because global_variables seems to be easier to understand than declare_interior_mutable_statics. If you still prefer your proposed name, I am OK to change it.

Also, I am not able to exclude statics with interior mutability but with “frozen” initializers. For example: A static using None::<AtomicU32> as the initializer can be excluded. This detection used for consts uses the is_value_unfrozen_poly function, which panics for statics. What should I do from here?

  • Ignore this false positive, since using a interior mutable static that isn’t actually mutable does not make much sense, the impact could be minor, or
  • Continue trying to figure out how to detect the case with non-mutable value case.

@rustbot

This comment has been minimized.

@Jarcho
Copy link
Contributor

Jarcho commented Sep 18, 2025

@EFanZh Sorry for the delay. The lint pass has been completely rewritten so you will need to rebase. You still won't be able to use the MIR value, but you can now use is_init_expr_freeze to evaluate the HIR expression used to initialize the static.

@EFanZh EFanZh force-pushed the add-global-variables-lint branch from 75b29dd to 318f6fa Compare October 1, 2025 03:34
@rustbot
Copy link
Collaborator

rustbot commented Oct 1, 2025

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

Copy link

github-actions bot commented Oct 1, 2025

Lintcheck changes for 7638b39

Lint Added Removed Changed
clippy::global_variables 88 0 0

This comment will be updated if you push new changes

@EFanZh EFanZh force-pushed the add-global-variables-lint branch from 318f6fa to 7638b39 Compare October 1, 2025 04:29
},
}
&& !item.span.in_external_macro(cx.sess().source_map())
let ty = || cx.tcx.type_of(item.owner_id).instantiate_identity();
Copy link
Contributor

Choose a reason for hiding this comment

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

This shouldn't be too expensive, feel free to evaluate it eagerly

Copy link
Author

@EFanZh EFanZh Oct 13, 2025

Choose a reason for hiding this comment

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

The type can only be queried when the item is of certain types, like const or static, evaluating it unconditionally will cause crashes.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, that's subtle! That would be nice to have as a comment:)

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.

4 participants