xtask/tasks/fmt/lints/
cfg_target_arch.rs1use super::Lint;
8use super::LintCtx;
9use super::Lintable;
10use std::path::Path;
11use toml_edit::DocumentMut;
12
13const SUPPRESS: &str = "xtask-fmt allow-target-arch";
14
15const SUPPRESS_REASON_CPU_INTRINSIC: &str = "cpu-intrinsic";
17const SUPPRESS_REASON_SYS_CRATE: &str = "sys-crate";
20const SUPPRESS_REASON_DEPENDENCY: &str = "dependency";
22const SUPPRESS_REASON_ONEOFF_GUEST_ARCH_IMPL: &str = "oneoff-guest-arch-impl";
25const SUPPRESS_REASON_ONEOFF_FLOWEY: &str = "oneoff-flowey";
27const SUPPRESS_REASON_ONEOFF_PETRI_NATIVE_TEST_DEPS: &str = "oneoff-petri-native-test-deps";
29const SUPPRESS_REASON_ONEOFF_PETRI_HOST_ARCH: &str = "oneoff-petri-host-arch";
31
32fn has_suppress(s: &str) -> bool {
33 let Some((_, after)) = s.split_once(SUPPRESS) else {
34 return false;
35 };
36
37 let after = after.trim();
38 let justification = after.split(' ').next().unwrap();
39
40 matches!(
41 justification,
42 SUPPRESS_REASON_CPU_INTRINSIC
43 | SUPPRESS_REASON_SYS_CRATE
44 | SUPPRESS_REASON_DEPENDENCY
45 | SUPPRESS_REASON_ONEOFF_GUEST_ARCH_IMPL
46 | SUPPRESS_REASON_ONEOFF_FLOWEY
47 | SUPPRESS_REASON_ONEOFF_PETRI_NATIVE_TEST_DEPS
48 | SUPPRESS_REASON_ONEOFF_PETRI_HOST_ARCH
49 )
50}
51
52fn is_exempt(path: &Path) -> bool {
54 path.starts_with("guest_test_uefi")
70 || path.starts_with("openhcl/openhcl_boot")
71 || path.starts_with("openhcl/minimal_rt")
72 || path.starts_with("openhcl/minimal_rt_reloc")
73 || path.starts_with("openhcl/sidecar")
74 || path.starts_with("support")
75 || path.starts_with("tmk/simple_tmk")
76 || path.starts_with("tmk/tmk_core")
77 || path.starts_with("opentmk")
78 || path.starts_with("vm/whp")
79 || path.starts_with("vm/kvm")
80}
81
82pub struct CfgTargetArch;
83
84impl Lint for CfgTargetArch {
85 fn new(_ctx: &LintCtx) -> Self {
86 CfgTargetArch
87 }
88
89 fn enter_workspace(&mut self, _content: &Lintable<DocumentMut>) {}
90 fn enter_crate(&mut self, _content: &Lintable<DocumentMut>) {}
91
92 fn visit_file(&mut self, content: &mut Lintable<String>) {
93 let path = content.path();
94
95 if path.ends_with(file!()) || is_exempt(path) {
97 return;
98 }
99
100 let mut prev_line = "";
101 for (i, line) in content.lines().enumerate() {
102 if line.contains("target_arch =") || line.contains("CARGO_CFG_TARGET_ARCH") {
103 if !line.trim().starts_with("//") && !has_suppress(line) && !has_suppress(prev_line)
105 {
106 content.unfixable(&format!(
107 "unjustified `cfg(target_arch = ...)` at line {}",
108 i + 1
109 ));
110 }
111 }
112 prev_line = line;
113 }
114 }
115
116 fn exit_crate(&mut self, _content: &mut Lintable<DocumentMut>) {}
117 fn exit_workspace(&mut self, _content: &mut Lintable<DocumentMut>) {}
118}
119
120#[cfg(test)]
121mod tests {
122 use super::super::test_support::failed;
123 use super::super::test_support::lintable;
124 use super::*;
125
126 fn fails(path: &str, text: &str) -> bool {
127 let mut file = lintable(path, text.to_owned(), false);
128 CfgTargetArch.visit_file(&mut file);
129 failed(&file)
130 }
131
132 #[test]
137 fn requires_known_adjacent_justification_outside_exempt_paths() {
138 let p = "vm/devices/x/src/lib.rs";
139 let cfg = "#[cfg(target_arch = \"x86_64\")]";
140 assert!(fails(p, cfg));
141 assert!(fails(p, "let a = env!(\"CARGO_CFG_TARGET_ARCH\");"));
142 assert!(!fails(
143 p,
144 &format!("{cfg} // xtask-fmt allow-target-arch sys-crate")
145 ));
146 assert!(!fails(
147 p,
148 &format!("// xtask-fmt allow-target-arch cpu-intrinsic\n{cfg}")
149 ));
150 assert!(fails(
151 p,
152 &format!("// xtask-fmt allow-target-arch cpu-intrinsic\n\n{cfg}")
153 ));
154 assert!(fails(
155 p,
156 &format!("{cfg} // xtask-fmt allow-target-arch because")
157 ));
158 assert!(fails(p, &format!("{cfg} // xtask-fmt allow-target-arch")));
159 assert!(!fails(p, &format!(" // {cfg}")));
160 assert!(!fails(p, "#[cfg(guest_arch = \"x86_64\")]"));
161 for exempt in [
162 "support/a/src/lib.rs",
163 "opentmk/x/src/lib.rs",
164 "vm/kvm/src/lib.rs",
165 ] {
166 assert!(!fails(exempt, cfg), "{exempt}");
167 }
168 assert!(fails("vm/kvmx/src/lib.rs", cfg));
169 }
170}