//! `cargo xtask cleanroom`: guards the clean-room rule of AGENTS.md. //! //! Fails on files that carry a JetBrains copyright notice, on files that look //! like exported JetBrains configuration (keymaps, colour schemes, live //! templates, inspection profiles, `.idea` project files) and on archives that //! could hold IDE internals (`.jar`, `.icls`, `.jfr`). //! //! The checker's own source necessarily contains the patterns it looks for, //! so this file is exempt from the content scan. use std::fs; use crate::util::{TaskResult, repo_files, repo_root}; /// This file, exempt from the content scan because it holds the patterns. const SELF_PATH: &str = "xtask/src/cleanroom.rs"; /// Extensions that are rejected outright. const FORBIDDEN_EXTENSIONS: &[&str] = &["jar", "icls", "jfr", "ipr", "iml", "iws"]; /// Markers of exported IDE configuration XML, matched case-sensitively. /// A file is flagged when it contains any one of them. const CONFIG_MARKERS: &[&str] = &[ "", " TaskResult { let root = repo_root()?; let files = repo_files(&root)?; let mut problems = Vec::new(); for file in &files { problems.extend(check_path(file)); if file == SELF_PATH { continue; } let path = root.join(file); let small = fs::metadata(&path) .map(|m| m.len() <= MAX_SCAN_BYTES) .unwrap_or(false); if !small { continue; } if let Ok(bytes) = fs::read(&path) { problems.extend(check_content(file, &String::from_utf8_lossy(&bytes))); } } if problems.is_empty() { println!( "cleanroom: {} files scanned, nothing suspicious", files.len() ); Ok(()) } else { Err(format!("clean-room problems:\n {}", problems.join("\n "))) } } /// Checks a repository path by name alone. pub fn check_path(path: &str) -> Vec { let mut problems = Vec::new(); if path.split('/').any(|part| part == ".idea") { problems.push(format!("{path}: .idea project files are never committed")); } let ext = path .rsplit_once('.') .map(|(_, e)| e.to_ascii_lowercase()) .unwrap_or_default(); if FORBIDDEN_EXTENSIONS.contains(&ext.as_str()) { problems.push(format!( "{path}: .{ext} files may contain JetBrains material" )); } problems } /// Whether a line holds a copyright notice in the name of JetBrains: a /// marker (`copyright`, `(c)` or `©`), then only further markers, years and /// punctuation, then `jetbrains`, in any case. Mentions such as "JetBrains /// copyright notices" don't match. pub fn is_jetbrains_notice(line: &str) -> bool { let lower = line.to_lowercase(); ["copyright", "(c)", "©"].iter().any(|marker| { lower.match_indices(marker).any(|(at, word)| { let mut rest = lower.get(at + word.len()..).unwrap_or_default(); loop { let trimmed = rest.trim_start_matches(|c: char| { c.is_ascii_digit() || matches!(c, ' ' | '-' | ',') }); let stripped = trimmed .strip_prefix("copyright") .or_else(|| trimmed.strip_prefix("(c)")) .or_else(|| trimmed.strip_prefix('©')); match stripped { Some(next) => rest = next, None => return trimmed.starts_with("jetbrains"), } } }) }) } /// Checks the text of one file. pub fn check_content(path: &str, text: &str) -> Vec { let mut problems = Vec::new(); for (n, line) in text.lines().enumerate() { if is_jetbrains_notice(line) { problems.push(format!("{path}:{}: JetBrains copyright notice", n + 1)); } } for marker in CONFIG_MARKERS { if text.contains(marker) { problems.push(format!( "{path}: looks like exported JetBrains configuration ({marker})" )); } } problems } #[cfg(test)] mod tests { use super::*; #[test] fn flags_copyright_notices() { let text = "// Copyright 2000-2026 JetBrains s.r.o. and contributors.\nfn main() {}"; assert_eq!(check_content("a.rs", text).len(), 1); assert!(is_jetbrains_notice("Copyright (c) JetBrains s.r.o.")); assert!(is_jetbrains_notice("(c) 2010-2024 JetBrains")); assert!(is_jetbrains_notice("Copyright © 2024 JETBRAINS")); } #[test] fn ignores_mentions_of_notices() { assert!(!is_jetbrains_notice("fails on JetBrains copyright notices")); assert!(!is_jetbrains_notice( "cleanroom check for JetBrains copyright notices" )); } #[test] fn allows_trademark_disclaimers_and_plain_mentions() { let text = "PhpStorm and JetBrains are trademarks of JetBrains s.r.o.\nCopyright (c) 2026 Daniel Diekert\nNever launch a JetBrains IDE."; assert!(check_content("NOTICE", text).is_empty()); } #[test] fn flags_exported_configuration() { assert_eq!( check_content("k.xml", "").len(), 1 ); assert_eq!( check_content("t.xml", "").len(), 1 ); } #[test] fn flags_forbidden_paths() { assert_eq!(check_path(".idea/workspace.xml").len(), 1); assert_eq!(check_path("lib/platform.jar").len(), 1); assert_eq!(check_path("themes/dark.icls").len(), 1); assert!(check_path("crates/engine/src/lib.rs").is_empty()); } }