Skip to content

Commit ee091f4

Browse files
authored
Add lint pub_static_mut_now_immutable (#768)
* Add lint pub_static_mut_now_immutable * Modifications in error message and description * Modifications in description and error message * Modifications in description and error message * Modifications in description and error message
1 parent 44d5e0b commit ee091f4

9 files changed

+150
-0
lines changed
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
SemverQuery(
2+
id: "pub_static_mut_now_immutable",
3+
human_readable_name: "pub static mut is now immutable",
4+
description: "A mutable static became immutable and thus can no longer be assigned to",
5+
required_update: Major,
6+
reference_link: Some("https://doc.rust-lang.org/reference/items/static-items.html"),
7+
query: r#"
8+
{
9+
CrateDiff {
10+
baseline {
11+
item {
12+
... on Static {
13+
visibility_limit @filter(op: "=", value: ["$public"])
14+
mutable @filter(op: "=", value: ["$true"])
15+
16+
importable_path {
17+
path @output @tag
18+
public_api @filter(op: "=", value: ["$true"])
19+
}
20+
}
21+
}
22+
}
23+
current {
24+
item {
25+
... on Static {
26+
visibility_limit @filter(op: "=", value: ["$public"])
27+
mutable @filter(op: "!=", value: ["$true"])
28+
static_name: name @output
29+
30+
importable_path {
31+
path @filter(op: "=", value: ["%path"])
32+
public_api @filter(op: "=", value: ["$true"])
33+
}
34+
35+
span_: span @optional {
36+
filename @output
37+
begin_line @output
38+
}
39+
}
40+
}
41+
}
42+
}
43+
}"#,
44+
arguments: {
45+
"public": "public",
46+
"true": true,
47+
},
48+
error_message: "A mutable static is now immutable and thus can no longer be assigned to",
49+
per_result_error_template: Some("{{static_name}} in file {{span_filename}}:{{span_begin_line}}"),
50+
)

src/query.rs

+1
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ add_lints!(
516516
pub_module_level_const_missing,
517517
pub_module_level_const_now_doc_hidden,
518518
pub_static_missing,
519+
pub_static_mut_now_immutable,
519520
pub_static_now_doc_hidden,
520521
repr_c_removed,
521522
repr_packed_added,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
publish = false
3+
name = "pub_static_mut_now_immutable"
4+
version = "0.1.0"
5+
edition = "2021"
6+
7+
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Basic Test cases
2+
pub static STATIC_A: i32 = 0;
3+
pub static mut STATIC_B: i32 = 0;
4+
static STATIC_C: i32 = 0;
5+
6+
// Test case for #[doc(hidden)] pub static
7+
#[doc(hidden)]
8+
pub static DOC_HIDDEN_STATIC_A: i32 = 0;
9+
10+
// Renaming or making a static #[doc(hidden)] along with making it immutable
11+
// should trigger only one lint
12+
#[doc(hidden)]
13+
pub static DOC_HIDDEN_STATIC_B: i32 = 0;
14+
pub static STATIC_RENAMED: i32 = 0;
15+
16+
// Testing for static defined in private module
17+
mod PRIVATE_MODULE {
18+
pub static STATIC_C: i32 = 0;
19+
}
20+
21+
// Testing for static defined in #[doc(hidden)] module
22+
#[doc(hidden)]
23+
pub mod DOC_HIDDEN_MODULE {
24+
pub static STATIC_C: i32 = 0;
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
publish = false
3+
name = "pub_static_mut_now_immutable"
4+
version = "0.1.0"
5+
edition = "2021"
6+
7+
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Basic Test cases
2+
pub static mut STATIC_A: i32 = 0;
3+
pub static mut STATIC_B: i32 = 0;
4+
static mut STATIC_C: i32 = 0;
5+
6+
// Test case for #[doc(hidden)] pub static
7+
#[doc(hidden)]
8+
pub static mut DOC_HIDDEN_STATIC_A: i32 = 0;
9+
10+
// Renaming or making a static #[doc(hidden)] along with making it immutable
11+
// should trigger only one lint
12+
pub static mut DOC_HIDDEN_STATIC_B: i32 = 0;
13+
pub static mut STATIC_RENAME: i32 = 0;
14+
15+
// Testing for static defined in private module
16+
mod PRIVATE_MODULE {
17+
pub static mut STATIC_C: i32 = 0;
18+
}
19+
20+
// Testing for static defined in #[doc(hidden)] module
21+
#[doc(hidden)]
22+
pub mod DOC_HIDDEN_MODULE {
23+
pub static mut STATIC_C: i32 = 0;
24+
}

test_outputs/pub_static_missing.output.ron

+12
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,16 @@
133133
"visibility_limit": String("public"),
134134
},
135135
],
136+
"./test_crates/pub_static_mut_now_immutable/": [
137+
{
138+
"name": String("STATIC_RENAME"),
139+
"path": List([
140+
String("pub_static_mut_now_immutable"),
141+
String("STATIC_RENAME"),
142+
]),
143+
"span_begin_line": Uint64(13),
144+
"span_filename": String("src/lib.rs"),
145+
"visibility_limit": String("public"),
146+
},
147+
],
136148
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"./test_crates/pub_static_mut_now_immutable/": [
3+
{
4+
"path": List([
5+
String("pub_static_mut_now_immutable"),
6+
String("STATIC_A"),
7+
]),
8+
"span_begin_line": Uint64(2),
9+
"span_filename": String("src/lib.rs"),
10+
"static_name": String("STATIC_A"),
11+
},
12+
],
13+
}

test_outputs/pub_static_now_doc_hidden.output.ron

+11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
{
2+
"./test_crates/pub_static_mut_now_immutable/": [
3+
{
4+
"path": List([
5+
String("pub_static_mut_now_immutable"),
6+
String("DOC_HIDDEN_STATIC_B"),
7+
]),
8+
"span_begin_line": Uint64(13),
9+
"span_filename": String("src/lib.rs"),
10+
"static_name": String("DOC_HIDDEN_STATIC_B"),
11+
},
12+
],
213
"./test_crates/pub_static_now_doc_hidden/": [
314
{
415
"path": List([

0 commit comments

Comments
 (0)