-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add global_variables
lint
#14657
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
Open
EFanZh
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
EFanZh:add-global-variables-lint
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add global_variables
lint
#14657
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#![warn(clippy::global_variables)] | ||
|
||
use std::sync::Mutex; | ||
use std::sync::atomic::AtomicU32; | ||
|
||
macro_rules! define_global_variables_with_macro { | ||
() => { | ||
static GLOBAL_VARIABLE_0: AtomicU32 = AtomicU32::new(2); | ||
//~^ global_variables | ||
|
||
static GLOBAL_VARIABLE_1: Mutex<u32> = Mutex::new(3); | ||
//~^ global_variables | ||
}; | ||
} | ||
|
||
#[allow(clippy::missing_const_for_thread_local)] | ||
fn main() { | ||
define_global_variables_with_macro!(); | ||
|
||
static GLOBAL_VARIABLE_2: AtomicU32 = AtomicU32::new(2); | ||
//~^ global_variables | ||
|
||
static GLOBAL_VARIABLE_3: Mutex<u32> = Mutex::new(3); | ||
//~^ global_variables | ||
|
||
static GLOBAL_VARIABLE_4: Option<AtomicU32> = Some(AtomicU32::new(0)); | ||
//~^ global_variables | ||
|
||
static NOT_GLOBAL_VARIABLE_0: u32 = 1; | ||
|
||
// ZST fields (like `PhantomData`) should be ignored. | ||
static NOT_GLOBAL_VARIABLE_1: Vec<AtomicU32> = Vec::new(); | ||
|
||
// Having a initializer with a freeze value should be ignored. | ||
static NOT_GLOBAL_VARIABLE_2: Option<AtomicU32> = None; | ||
|
||
// Thread-local variables are ignored. | ||
thread_local! { | ||
static THREAD_LOCAL_VARIABLE_0: u32 = 0; | ||
static THREAD_LOCAL_VARIABLE_1: AtomicU32 = AtomicU32::new(0); | ||
static THREAD_LOCAL_VARIABLE_2: u32 = const { 0 }; | ||
static THREAD_LOCAL_VARIABLE_3: AtomicU32 = const { AtomicU32::new(0) }; | ||
|
||
static THREAD_LOCAL_VARIABLE_4: () = { | ||
// Global variables inside a thread-local initializer are also considered. | ||
static GLOBAL_VARIABLE_IN_THREAD_LOCAL: AtomicU32 = AtomicU32::new(0); | ||
//~^ global_variables | ||
}; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
error: global variable should not be used | ||
--> tests/ui/global_variables.rs:8:16 | ||
| | ||
LL | static GLOBAL_VARIABLE_0: AtomicU32 = AtomicU32::new(2); | ||
| ^^^^^^^^^^^^^^^^^ | ||
... | ||
LL | define_global_variables_with_macro!(); | ||
| ------------------------------------- in this macro invocation | ||
| | ||
= help: consider passing this as a function argument or using a `thread_local` | ||
= note: `-D clippy::global-variables` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::global_variables)]` | ||
= note: this error originates in the macro `define_global_variables_with_macro` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: global variable should not be used | ||
--> tests/ui/global_variables.rs:11:16 | ||
| | ||
LL | static GLOBAL_VARIABLE_1: Mutex<u32> = Mutex::new(3); | ||
| ^^^^^^^^^^^^^^^^^ | ||
... | ||
LL | define_global_variables_with_macro!(); | ||
| ------------------------------------- in this macro invocation | ||
| | ||
= help: consider passing this as a function argument or using a `thread_local` | ||
= note: this error originates in the macro `define_global_variables_with_macro` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: global variable should not be used | ||
--> tests/ui/global_variables.rs:20:12 | ||
| | ||
LL | static GLOBAL_VARIABLE_2: AtomicU32 = AtomicU32::new(2); | ||
| ^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider passing this as a function argument or using a `thread_local` | ||
|
||
error: global variable should not be used | ||
--> tests/ui/global_variables.rs:23:12 | ||
| | ||
LL | static GLOBAL_VARIABLE_3: Mutex<u32> = Mutex::new(3); | ||
| ^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider passing this as a function argument or using a `thread_local` | ||
|
||
error: global variable should not be used | ||
--> tests/ui/global_variables.rs:26:12 | ||
| | ||
LL | static GLOBAL_VARIABLE_4: Option<AtomicU32> = Some(AtomicU32::new(0)); | ||
| ^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider passing this as a function argument or using a `thread_local` | ||
|
||
error: global variable should not be used | ||
--> tests/ui/global_variables.rs:46:20 | ||
| | ||
LL | static GLOBAL_VARIABLE_IN_THREAD_LOCAL: AtomicU32 = AtomicU32::new(0); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider passing this as a function argument or using a `thread_local` | ||
|
||
error: aborting due to 6 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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
orstatic
, evaluating it unconditionally will cause crashes.There was a problem hiding this comment.
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:)