Skip to content

Commit

Permalink
feat: add pub_static_now_mutable lint (#1020)
Browse files Browse the repository at this point in the history
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
orhun authored Dec 7, 2024
1 parent a37a3f7 commit a7e8c89
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/lints/pub_static_now_mutable.ron
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}}"),
)
1 change: 1 addition & 0 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,7 @@ add_lints!(
pub_static_missing,
pub_static_mut_now_immutable,
pub_static_now_doc_hidden,
pub_static_now_mutable,
repr_c_removed,
repr_packed_added,
repr_packed_removed,
Expand Down
7 changes: 7 additions & 0 deletions test_crates/pub_static_now_mutable/new/Cargo.toml
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]
24 changes: 24 additions & 0 deletions test_crates/pub_static_now_mutable/new/src/lib.rs
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;
}
7 changes: 7 additions & 0 deletions test_crates/pub_static_now_mutable/old/Cargo.toml
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]
25 changes: 25 additions & 0 deletions test_crates/pub_static_now_mutable/old/src/lib.rs
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;
}
13 changes: 13 additions & 0 deletions test_outputs/query_execution/pub_static_missing.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: src/query.rs
expression: "&query_execution_results"
snapshot_kind: text
---
{
"./test_crates/pub_static_missing/": [
Expand Down Expand Up @@ -149,4 +150,16 @@ expression: "&query_execution_results"
"visibility_limit": String("public"),
},
],
"./test_crates/pub_static_now_mutable/": [
{
"name": String("STATIC_RENAMED"),
"path": List([
String("pub_static_now_mutable"),
String("STATIC_RENAMED"),
]),
"span_begin_line": Uint64(14),
"span_filename": String("src/lib.rs"),
"visibility_limit": String("public"),
},
],
}
18 changes: 18 additions & 0 deletions test_outputs/query_execution/pub_static_now_mutable.snap
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"),
},
],
}

0 comments on commit a7e8c89

Please sign in to comment.