-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add pub_static_now_mutable lint (#1020)
This new lint is for public static variables which might be mutable later on. This would require them to use an unsafe block which is breaking.
- Loading branch information
Showing
8 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,51 @@ | ||
SemverQuery( | ||
id: "pub_static_now_mutable", | ||
human_readable_name: "pub static is now mutable", | ||
description: "An immutable static became mutable and thus an unsafe block is required to use it", | ||
required_update: Major, | ||
lint_level: Deny, | ||
reference_link: Some("https://google.github.io/comprehensive-rust/unsafe-rust/mutable-static.html"), | ||
query: r#" | ||
{ | ||
CrateDiff { | ||
baseline { | ||
item { | ||
... on Static { | ||
visibility_limit @filter(op: "=", value: ["$public"]) | ||
mutable @filter(op: "!=", value: ["$true"]) | ||
importable_path { | ||
path @output @tag | ||
public_api @filter(op: "=", value: ["$true"]) | ||
} | ||
} | ||
} | ||
} | ||
current { | ||
item { | ||
... on Static { | ||
visibility_limit @filter(op: "=", value: ["$public"]) | ||
mutable @filter(op: "=", value: ["$true"]) | ||
static_name: name @output | ||
importable_path { | ||
path @filter(op: "=", value: ["%path"]) | ||
public_api @filter(op: "=", value: ["$true"]) | ||
} | ||
span_: span @optional { | ||
filename @output | ||
begin_line @output | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}"#, | ||
arguments: { | ||
"public": "public", | ||
"true": true, | ||
}, | ||
error_message: "An immutable static is now mutable and thus an unsafe block is required to use it", | ||
per_result_error_template: Some("{{static_name}} in file {{span_filename}}:{{span_begin_line}}"), | ||
) |
This file contains 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 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,7 @@ | ||
[package] | ||
publish = false | ||
name = "pub_static_now_mutable" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
This file contains 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,24 @@ | ||
// Basic Test cases | ||
pub static mut STATIC_A: i32 = 0; | ||
pub static mut STATIC_B: i32 = 0; | ||
static mut STATIC_C: i32 = 0; | ||
|
||
// Test case for #[doc(hidden)] pub static | ||
#[doc(hidden)] | ||
pub static mut DOC_HIDDEN_STATIC_A: i32 = 0; | ||
|
||
// Renaming or making a static #[doc(hidden)] along with making it mutable | ||
// should trigger only one lint | ||
pub static mut DOC_HIDDEN_STATIC_B: i32 = 0; | ||
pub static mut STATIC_RENAME: i32 = 0; | ||
|
||
// Testing for static defined in private module | ||
mod PRIVATE_MODULE { | ||
pub static mut STATIC_C: i32 = 0; | ||
} | ||
|
||
// Testing for static defined in #[doc(hidden)] module | ||
#[doc(hidden)] | ||
pub mod DOC_HIDDEN_MODULE { | ||
pub static mut STATIC_C: i32 = 0; | ||
} |
This file contains 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,7 @@ | ||
[package] | ||
publish = false | ||
name = "pub_static_now_mutable" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
This file contains 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,25 @@ | ||
// Basic Test cases | ||
pub static STATIC_A: i32 = 0; | ||
pub static mut STATIC_B: i32 = 0; | ||
static STATIC_C: i32 = 0; | ||
|
||
// Test case for #[doc(hidden)] pub static | ||
#[doc(hidden)] | ||
pub static DOC_HIDDEN_STATIC_A: i32 = 0; | ||
|
||
// Renaming or making a static #[doc(hidden)] along with making it mutable | ||
// should trigger only one lint | ||
#[doc(hidden)] | ||
pub static DOC_HIDDEN_STATIC_B: i32 = 0; | ||
pub static STATIC_RENAMED: i32 = 0; | ||
|
||
// Testing for static defined in private module | ||
mod PRIVATE_MODULE { | ||
pub static STATIC_C: i32 = 0; | ||
} | ||
|
||
// Testing for static defined in #[doc(hidden)] module | ||
#[doc(hidden)] | ||
pub mod DOC_HIDDEN_MODULE { | ||
pub static STATIC_C: i32 = 0; | ||
} |
This file contains 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 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,18 @@ | ||
--- | ||
source: src/query.rs | ||
expression: "&query_execution_results" | ||
snapshot_kind: text | ||
--- | ||
{ | ||
"./test_crates/pub_static_now_mutable/": [ | ||
{ | ||
"path": List([ | ||
String("pub_static_now_mutable"), | ||
String("STATIC_A"), | ||
]), | ||
"span_begin_line": Uint64(2), | ||
"span_filename": String("src/lib.rs"), | ||
"static_name": String("STATIC_A"), | ||
}, | ||
], | ||
} |