src/tools/tidy/src/style.rs RUST 717 lines View on github.com → Search inside
1//! Tidy check to enforce various stylistic guidelines on the Rust codebase.2//!3//! Example checks are:4//!5//! * No lines over 100 characters (in non-Rust files).6//! * No files with over 3000 lines (in non-Rust files).7//! * No tabs.8//! * No trailing whitespace.9//! * No CR characters.10//! * No `TODO` or `XXX` directives.11//! * No unexplained ` ```ignore ` or ` ```rust,ignore ` doc tests.12//!13//! Note that some of these rules are excluded from Rust files because we enforce rustfmt. It is14//! preferable to be formatted rather than tidy-clean.15//!16//! A number of these checks can be opted-out of with various directives of the form:17//! `// ignore-tidy-CHECK-NAME`.18// ignore-tidy-dbg1920use std::ffi::OsStr;21use std::path::Path;22use std::sync::LazyLock;2324use regex::RegexSetBuilder;25use rustc_hash::FxHashMap;2627use crate::diagnostics::{CheckId, TidyCtx};28use crate::walk::{filter_dirs, walk};2930#[cfg(test)]31mod tests;3233/// Error code markdown is restricted to 80 columns because they can be34/// displayed on the console with --example.35const ERROR_CODE_COLS: usize = 80;36const COLS: usize = 100;37const GOML_COLS: usize = 120;3839const LINES: usize = 3000;4041const UNEXPLAINED_IGNORE_DOCTEST_INFO: &str = r#"unexplained "```ignore" doctest; try one:4243* make the test actually pass, by adding necessary imports and declarations, or44* use "```text", if the code is not Rust code, or45* use "```compile_fail,Ennnn", if the code is expected to fail at compile time, or46* use "```should_panic", if the code is expected to fail at run time, or47* use "```no_run", if the code should type-check but not necessary linkable/runnable, or48* explain it like "```ignore (cannot-test-this-because-xxxx)", if the annotation cannot be avoided.4950"#;5152const LLVM_UNREACHABLE_INFO: &str = r"\53C++ code used llvm_unreachable, which triggers undefined behavior54when executed when assertions are disabled.55Use llvm::report_fatal_error for increased robustness.";5657const DOUBLE_SPACE_AFTER_DOT: &str = r"\58Use a single space after dots in comments.";5960const ANNOTATIONS_TO_IGNORE: &[&str] = &[61    "// @!has",62    "// @has",63    "// @matches",64    "// CHECK",65    "// EMIT_MIR",66    "// compile-flags",67    "//@ compile-flags",68    "// error-pattern",69    "//@ error-pattern",70    "//@ gdb",71    "//@ lldb",72    "//@ cdb",73    "//@ normalize-stderr",74];7576const LINELENGTH_CHECK: &str = "linelength";7778// If you edit this, also edit where it gets used in `check` (calling `contains_ignore_directives`)79const CONFIGURABLE_CHECKS: [&str; 11] = [80    "cr",81    "undocumented-unsafe",82    "tab",83    LINELENGTH_CHECK,84    "filelength",85    "end-whitespace",86    "trailing-newlines",87    "leading-newlines",88    "copyright",89    "dbg",90    "odd-backticks",91];9293fn generate_problems<'a>(94    consts: &'a [u32],95    letter_digit: &'a FxHashMap<char, char>,96) -> impl Iterator<Item = u32> + 'a {97    consts.iter().flat_map(move |const_value| {98        let problem = letter_digit.iter().fold(format!("{const_value:X}"), |acc, (key, value)| {99            acc.replace(&value.to_string(), &key.to_string())100        });101        let indexes: Vec<usize> = problem102            .chars()103            .enumerate()104            .filter_map(|(index, c)| if letter_digit.contains_key(&c) { Some(index) } else { None })105            .collect();106        (0..1 << indexes.len()).map(move |i| {107            u32::from_str_radix(108                &problem109                    .chars()110                    .enumerate()111                    .map(|(index, c)| {112                        if let Some(pos) = indexes.iter().position(|&x| x == index) {113                            if (i >> pos) & 1 == 1 { letter_digit[&c] } else { c }114                        } else {115                            c116                        }117                    })118                    .collect::<String>(),119                0x10,120            )121            .unwrap()122        })123    })124}125126// Intentionally written in decimal rather than hex127const ROOT_PROBLEMATIC_CONSTS: &[u32] = &[128    184594741, 2880289470, 2881141438, 2965027518, 2976579765, 3203381950, 3405691582, 3405697037,129    3735927486, 3735932941, 4027431614, 4276992702, 195934910, 252707358, 762133, 179681982,130    173390526, 721077,131];132133const LETTER_DIGIT: &[(char, char)] = &[('A', '4'), ('B', '8'), ('E', '3')];134135// Returns all permutations of problematic consts, over 2000 elements.136fn generate_problematic_strings(137    consts: &[u32],138    letter_digit: &FxHashMap<char, char>,139) -> Vec<String> {140    generate_problems(consts, letter_digit)141        .flat_map(|v| vec![v.to_string(), format!("{:X}", v)])142        .collect()143}144145static PROBLEMATIC_CONSTS_STRINGS: LazyLock<Vec<String>> = LazyLock::new(|| {146    generate_problematic_strings(ROOT_PROBLEMATIC_CONSTS, &LETTER_DIGIT.iter().cloned().collect())147});148149fn contains_problematic_const(trimmed: &str) -> bool {150    PROBLEMATIC_CONSTS_STRINGS.iter().any(|s| trimmed.to_uppercase().contains(s))151}152153const INTERNAL_COMPILER_DOCS_LINE: &str = "#### This error code is internal to the compiler and will not be emitted with normal Rust code.";154155/// Parser states for `line_is_url`.156#[derive(Clone, Copy, PartialEq)]157#[allow(non_camel_case_types)]158enum LIUState {159    EXP_COMMENT_START,160    EXP_LINK_LABEL_OR_URL,161    EXP_URL,162    EXP_END,163}164165/// Returns `true` if `line` appears to be a line comment containing a URL,166/// possibly with a Markdown link label in front, and nothing else.167/// The Markdown link label, if present, may not contain whitespace.168/// Lines of this form are allowed to be overlength, because Markdown169/// offers no way to split a line in the middle of a URL, and the lengths170/// of URLs to external references are beyond our control.171fn line_is_url(is_error_code: bool, columns: usize, line: &str) -> bool {172    // more basic check for markdown, to avoid complexity in implementing two state machines173    if is_error_code {174        return line.starts_with('[') && line.contains("]:") && line.contains("http");175    }176177    use self::LIUState::*;178    let mut state: LIUState = EXP_COMMENT_START;179    let is_url = |w: &str| w.starts_with("http://") || w.starts_with("https://");180181    for tok in line.split_whitespace() {182        match (state, tok) {183            (EXP_COMMENT_START, "//") | (EXP_COMMENT_START, "///") | (EXP_COMMENT_START, "//!") => {184                state = EXP_LINK_LABEL_OR_URL185            }186187            (EXP_LINK_LABEL_OR_URL, w)188                if w.len() >= 4 && w.starts_with('[') && w.ends_with("]:") =>189            {190                state = EXP_URL191            }192193            (EXP_LINK_LABEL_OR_URL, w) if is_url(w) => state = EXP_END,194195            (EXP_URL, w) if is_url(w) || w.starts_with("../") => state = EXP_END,196197            (_, w) if w.len() > columns && is_url(w) => state = EXP_END,198199            (_, _) => {}200        }201    }202203    state == EXP_END204}205206/// Returns `true` if `line` can be ignored. This is the case when it contains207/// an annotation that is explicitly ignored.208fn should_ignore(line: &str) -> bool {209    // Matches test annotations like `//~ ERROR text`.210    // This mirrors the regex in src/tools/compiletest/src/runtest.rs, please211    // update both if either are changed.212    static_regex!("\\s*//(\\[.*\\])?~.*").is_match(line)213        || ANNOTATIONS_TO_IGNORE.iter().any(|a| line.contains(a))214215    // For `ui_test`-style UI test directives, also ignore216    // - `//@[rev] compile-flags`217    // - `//@[rev] normalize-stderr`218        || static_regex!("\\s*//@(\\[.*\\]) (compile-flags|normalize-stderr|error-pattern).*")219            .is_match(line)220        // Matching for rustdoc tests commands.221        // It allows to prevent them emitting warnings like `line longer than 100 chars`.222        || static_regex!(223            "\\s*//@ \\!?(count|files|has|has-dir|hasraw|matches|matchesraw|snapshot)\\s.*"224        ).is_match(line)225        // Matching for FileCheck checks226        || static_regex!(227            "\\s*// [a-zA-Z0-9-_]*:\\s.*"228        ).is_match(line)229}230231/// Returns `true` if `line` is allowed to be longer than the normal limit.232fn long_line_is_ok(extension: &str, is_error_code: bool, max_columns: usize, line: &str) -> bool {233    match extension {234        // non-error code markdown is allowed to be any length235        "md" if !is_error_code => true,236        // HACK(Ezrashaw): there is no way to split a markdown header over multiple lines237        "md" if line == INTERNAL_COMPILER_DOCS_LINE => true,238        _ => line_is_url(is_error_code, max_columns, line) || should_ignore(line),239    }240}241242#[derive(Clone, Copy)]243enum Directive {244    /// By default, tidy always warns against style issues.245    Deny,246247    /// `Ignore(false)` means that an `ignore-tidy-*` directive248    /// has been provided, but is unnecessary. `Ignore(true)`249    /// means that it is necessary (i.e. a warning would be250    /// produced if `ignore-tidy-*` was not present).251    Ignore(bool),252}253254// Use a fixed size array in the return type to catch mistakes with changing `CONFIGURABLE_CHECKS`255// without changing the code in `check` easier.256fn contains_ignore_directives<const N: usize>(257    path_str: &str,258    can_contain: bool,259    contents: &str,260    checks: [&str; N],261) -> [Directive; N] {262    // The rustdoc-json test syntax often requires very long lines, so the checks263    // for long lines aren't really useful.264    let always_ignore_linelength = path_str.contains("rustdoc-json");265266    if !can_contain && !always_ignore_linelength {267        return [Directive::Deny; N];268    }269270    checks.map(|check| {271        if check == LINELENGTH_CHECK && always_ignore_linelength {272            return Directive::Ignore(false);273        }274275        // Update `can_contain` when changing this276        if contents.contains(&format!("// ignore-tidy-{check}"))277            || contents.contains(&format!("# ignore-tidy-{check}"))278            || contents.contains(&format!("/* ignore-tidy-{check} */"))279            || contents.contains(&format!("<!-- ignore-tidy-{check} -->"))280        {281            Directive::Ignore(false)282        } else {283            Directive::Deny284        }285    })286}287288macro_rules! suppressible_tidy_err {289    ($err:ident, $skip:ident, $msg:literal) => {290        if let Directive::Deny = $skip {291            $err(&format!($msg));292        } else {293            $skip = Directive::Ignore(true);294        }295    };296}297298pub fn is_in(full_path: &Path, parent_folder_to_find: &str, folder_to_find: &str) -> bool {299    if let Some(parent) = full_path.parent() {300        if parent.file_name().map_or_else(301            || false,302            |f| {303                f == folder_to_find304                    && parent305                        .parent()306                        .and_then(|f| f.file_name())307                        .map_or_else(|| false, |f| f == parent_folder_to_find)308            },309        ) {310            true311        } else {312            is_in(parent, parent_folder_to_find, folder_to_find)313        }314    } else {315        false316    }317}318319fn skip_markdown_path(path: &Path) -> bool {320    // These aren't ready for tidy.321    const SKIP_MD: &[&str] = &[322        "src/doc/edition-guide",323        "src/doc/embedded-book",324        "src/doc/nomicon",325        "src/doc/reference",326        "src/doc/rust-by-example",327        "src/doc/rustc-dev-guide",328    ];329    SKIP_MD.iter().any(|p| path.ends_with(p))330}331332fn is_unexplained_ignore(extension: &str, line: &str) -> bool {333    if !line.ends_with("```ignore") && !line.ends_with("```rust,ignore") {334        return false;335    }336    if extension == "md" && line.trim().starts_with("//") {337        // Markdown examples may include doc comments with ignore inside a338        // code block.339        return false;340    }341    true342}343344pub fn check(path: &Path, tidy_ctx: TidyCtx) {345    let mut check = tidy_ctx.start_check(CheckId::new("style").path(path));346347    fn skip(path: &Path, is_dir: bool) -> bool {348        if path.file_name().is_some_and(|name| name.to_string_lossy().starts_with(".#")) {349            // vim or emacs temporary file350            return true;351        }352353        if filter_dirs(path) || skip_markdown_path(path) {354            return true;355        }356357        // Don't check extensions for directories358        if is_dir {359            return false;360        }361362        let extensions = ["rs", "py", "js", "sh", "c", "cpp", "h", "md", "css", "goml"];363364        // NB: don't skip paths without extensions (or else we'll skip all directories and will only check top level files)365        if path.extension().is_none_or(|ext| !extensions.iter().any(|e| ext == OsStr::new(e))) {366            return true;367        }368369        // We only check CSS files in rustdoc.370        path.extension().is_some_and(|e| e == "css") && !is_in(path, "src", "librustdoc")371    }372373    // This creates a RegexSet as regex contains performance optimizations to be able to deal with these over374    // 2000 needles efficiently. This runs over the entire source code, so performance matters.375    let problematic_regex = RegexSetBuilder::new(PROBLEMATIC_CONSTS_STRINGS.as_slice())376        .case_insensitive(true)377        .build()378        .unwrap();379380    // In some cases, a style check would be triggered by its own implementation381    // or comments. A simple workaround is to just allowlist this file.382    let this_file = Path::new(file!());383    let codegen_file = Path::new("src/tools/tidy/src/codegen.rs");384385    walk(path, skip, &mut |entry, contents| {386        let file = entry.path();387        let path_str = file.to_string_lossy();388        let filename = file.file_name().unwrap().to_string_lossy();389390        let is_css_file = filename.ends_with(".css");391        let under_rustfmt = filename.ends_with(".rs") &&392            // This list should ideally be sourced from rustfmt.toml but we don't want to add a toml393            // parser to tidy.394            !file.ancestors().any(|a| {395                (a.ends_with("tests") && a.join("COMPILER_TESTS.md").exists()) ||396                    a.ends_with("src/doc/book")397            });398399        if contents.is_empty() {400            check.error(format!("{}: empty file", file.display()));401        }402403        let extension = file.extension().unwrap().to_string_lossy();404        let is_error_code = extension == "md" && is_in(file, "src", "error_codes");405        let is_goml_code = extension == "goml";406407        let max_columns = if is_error_code {408            ERROR_CODE_COLS409        } else if is_goml_code {410            GOML_COLS411        } else {412            COLS413        };414415        // When you change this, also change the `directive_line_starts` variable below416        let can_contain = contents.contains("// ignore-tidy-")417            || contents.contains("# ignore-tidy-")418            || contents.contains("/* ignore-tidy-")419            || contents.contains("<!-- ignore-tidy-");420        // Enable testing ICE's that require specific (untidy)421        // file formats easily eg. `issue-1234-ignore-tidy.rs`422        if filename.contains("ignore-tidy") {423            return;424        }425        // Shell completions are automatically generated426        if let Some(p) = file.parent()427            && p.ends_with(Path::new("src/etc/completions"))428        {429            return;430        }431        let [432            mut skip_cr,433            mut skip_undocumented_unsafe,434            mut skip_tab,435            mut skip_line_length,436            mut skip_file_length,437            mut skip_end_whitespace,438            mut skip_trailing_newlines,439            mut skip_leading_newlines,440            mut skip_copyright,441            mut skip_dbg,442            mut skip_odd_backticks,443        ] = contains_ignore_directives(&path_str, can_contain, contents, CONFIGURABLE_CHECKS);444        let mut leading_new_lines = false;445        let mut trailing_new_lines = 0;446        let mut lines = 0;447        let mut last_safety_comment = false;448        let mut comment_block: Option<(usize, usize)> = None;449        let is_test = file.components().any(|c| c.as_os_str() == "tests")450            || file.file_stem().unwrap() == "tests";451        let is_codegen_test = is_test && file.components().any(|c| c.as_os_str() == "codegen-llvm");452        let is_this_file = file.ends_with(this_file) || this_file.ends_with(file);453        let is_test_for_this_file =454            is_test && file.parent().unwrap().ends_with(this_file.with_extension(""));455        let is_codegen_tidy_file = file.ends_with(codegen_file);456        // scanning the whole file for multiple needles at once is more efficient than457        // executing lines times needles separate searches.458        let any_problematic_line =459            !is_this_file && !is_test_for_this_file && problematic_regex.is_match(contents);460        for (i, line) in contents.split('\n').enumerate() {461            if line.is_empty() {462                if i == 0 {463                    leading_new_lines = true;464                }465                trailing_new_lines += 1;466                continue;467            } else {468                trailing_new_lines = 0;469            }470471            let trimmed = line.trim();472473            if !trimmed.starts_with("//") {474                lines += 1;475            }476477            let mut err = |msg: &str| {478                check.error(format!("{}:{}: {msg}", file.display(), i + 1));479            };480481            if trimmed.contains("dbg!")482                && !trimmed.starts_with("//")483                && !file.ancestors().any(|a| {484                    (a.ends_with("tests") && a.join("COMPILER_TESTS.md").exists())485                        || a.ends_with("library/alloctests")486                })487                && filename != "tests.rs"488            {489                suppressible_tidy_err!(490                    err,491                    skip_dbg,492                    "`dbg!` macro is intended as a debugging tool. It should not be in version control."493                )494            }495496            if is_codegen_test && trimmed.contains("CHECK") && trimmed.ends_with(": br") {497                err("`CHECK: br` and `CHECK-NOT: br` in codegen tests are fragile to false \498                    positives in mangled symbols. Try using `br {{.*}}` instead.")499            }500501            if !under_rustfmt502                && line.chars().count() > max_columns503                && !long_line_is_ok(&extension, is_error_code, max_columns, line)504            {505                suppressible_tidy_err!(506                    err,507                    skip_line_length,508                    "line longer than {max_columns} chars"509                );510            }511            if !is_css_file && line.contains('\t') {512                suppressible_tidy_err!(err, skip_tab, "tab character");513            }514            if line.ends_with(' ') || line.ends_with('\t') {515                suppressible_tidy_err!(err, skip_end_whitespace, "trailing whitespace");516            }517            if is_css_file && line.starts_with(' ') {518                err("CSS files use tabs for indent");519            }520            if line.contains('\r') {521                suppressible_tidy_err!(err, skip_cr, "CR character");522            }523            if !is_this_file && !is_codegen_tidy_file {524                let directive_line_starts = ["// ", "# ", "/* ", "<!-- "];525                let possible_line_start =526                    directive_line_starts.into_iter().any(|s| line.starts_with(s));527                let contains_potential_directive =528                    possible_line_start && (line.contains("-tidy") || line.contains("tidy-"));529                let has_recognized_ignore_directive =530                    contains_ignore_directives(&path_str, can_contain, line, CONFIGURABLE_CHECKS)531                        .into_iter()532                        .any(|directive| matches!(directive, Directive::Ignore(_)));533                let has_alphabetical_directive = line.contains("tidy-alphabetical-start")534                    || line.contains("tidy-alphabetical-end");535                let has_other_tidy_ignore_directive =536                    line.contains("ignore-tidy-target-specific-tests");537                let has_recognized_directive = has_recognized_ignore_directive538                    || has_alphabetical_directive539                    || has_other_tidy_ignore_directive;540                if contains_potential_directive && (!has_recognized_directive) {541                    err("Unrecognized tidy directive")542                }543                // Allow using TODO in diagnostic suggestions by marking the544                // relevant line with `// ignore-tidy-todo`.545                if trimmed.contains("TODO") && !trimmed.contains("ignore-tidy-todo") {546                    err(547                        "TODO is used for tasks that should be done before merging a PR; If you want to leave a message in the codebase use FIXME",548                    )549                }550                if trimmed.contains("//") && trimmed.contains(" XXX") {551                    err("Instead of XXX use FIXME")552                }553                if any_problematic_line && contains_problematic_const(trimmed) {554                    err("Don't use magic numbers that spell things (consider 0x12345678)");555                }556            }557            // for now we just check libcore558            if trimmed.contains("unsafe {")559                && !trimmed.starts_with("//")560                && !last_safety_comment561                && file.components().any(|c| c.as_os_str() == "core")562                && !is_test563            {564                suppressible_tidy_err!(err, skip_undocumented_unsafe, "undocumented unsafe");565            }566            if trimmed.contains("// SAFETY:") {567                last_safety_comment = true;568            } else if trimmed.starts_with("//") || trimmed.is_empty() {569                // keep previous value570            } else {571                last_safety_comment = false;572            }573            if (line.starts_with("// Copyright")574                || line.starts_with("# Copyright")575                || line.starts_with("Copyright"))576                && (trimmed.contains("Rust Developers")577                    || trimmed.contains("Rust Project Developers"))578            {579                suppressible_tidy_err!(580                    err,581                    skip_copyright,582                    "copyright notices attributed to the Rust Project Developers are deprecated"583                );584            }585            if !file.components().any(|c| c.as_os_str() == "rustc_baked_icu_data")586                && is_unexplained_ignore(&extension, line)587            {588                err(UNEXPLAINED_IGNORE_DOCTEST_INFO);589            }590591            if filename.ends_with(".cpp") && line.contains("llvm_unreachable") {592                err(LLVM_UNREACHABLE_INFO);593            }594595            // For now only enforce in compiler596            let is_compiler = || file.components().any(|c| c.as_os_str() == "compiler");597598            if is_compiler() {599                if line.contains("//")600                    && line601                        .chars()602                        .collect::<Vec<_>>()603                        .windows(4)604                        .any(|cs| matches!(cs, ['.', ' ', ' ', last] if last.is_alphabetic()))605                {606                    err(DOUBLE_SPACE_AFTER_DOT)607                }608609                // Heuristics for matching unbalanced backticks by trying to find comments and610                // comment blocks. Technically, this can have false negatives (or false positives),611                // but as a heuristic this is fine.612                let likely_comment = |trimmed: &str| {613                    // Line comments, doc comments614                    trimmed.contains("//")615                        // Also account for `#[cfg_attr(bootstrap, doc = "")]` cases.616                        || (trimmed.contains("cfg_attr") && trimmed.contains("doc"))617                };618619                if likely_comment(trimmed) {620                    let (start_line, mut backtick_count) = comment_block.unwrap_or((i + 1, 0));621                    let line_backticks = trimmed.chars().filter(|ch| *ch == '`').count();622623                    // Try to split `//`-like comments or `#[cfg_attr(bootstrap), doc = ""]`-like624                    // doc attributes. Fuzzy, but probably good enough.625                    let comment_text = match trimmed.split("//").nth(1) {626                        Some(text) => text,627                        None => {628                            // Fallback to try look for RHS of doc attr bits.629                            let (_doc, rest) =630                                trimmed.split_once("doc").expect("failed to find `doc` attribute");631                            rest632                        }633                    };634635                    // If backticks on a given comment line is not balanced, add to backtick count.636                    // This is to account for wrapped backticks and code blocks.637                    if line_backticks % 2 == 1 {638                        backtick_count += comment_text.chars().filter(|ch| *ch == '`').count();639                    }640                    comment_block = Some((start_line, backtick_count));641                } else if let Some((start_line, backtick_count)) = comment_block.take()642                    && backtick_count % 2 == 1643                {644                    let mut err = |msg: &str| {645                        check.error(format!("{}:{start_line}: {msg}", file.display()));646                    };647                    let block_len = (i + 1) - start_line;648                    if block_len == 1 {649                        suppressible_tidy_err!(650                            err,651                            skip_odd_backticks,652                            "comment with odd number of backticks"653                        );654                    } else {655                        suppressible_tidy_err!(656                            err,657                            skip_odd_backticks,658                            "{block_len}-line comment block with odd number of backticks"659                        );660                    }661                }662            }663        }664        if leading_new_lines {665            let mut err = |_| {666                check.error(format!("{}: leading newline", file.display()));667            };668            suppressible_tidy_err!(err, skip_leading_newlines, "missing leading newline");669        }670        let mut err = |msg: &str| {671            check.error(format!("{}: {}", file.display(), msg));672        };673        match trailing_new_lines {674            0 => suppressible_tidy_err!(err, skip_trailing_newlines, "missing trailing newline"),675            1 => {}676            n => suppressible_tidy_err!(677                err,678                skip_trailing_newlines,679                "too many trailing newlines ({n})"680            ),681        };682        if lines > LINES {683            let mut err = |_| {684                check.error(format!(685                    "{}: too many lines ({lines}) (add `// \686                     ignore-tidy-filelength` to the file to suppress this error)",687                    file.display(),688                ));689            };690            suppressible_tidy_err!(err, skip_file_length, "");691        }692693        if let Directive::Ignore(false) = skip_cr {694            check.error(format!("{}: ignoring CR characters unnecessarily", file.display()));695        }696        if let Directive::Ignore(false) = skip_tab {697            check.error(format!("{}: ignoring tab characters unnecessarily", file.display()));698        }699        if let Directive::Ignore(false) = skip_end_whitespace {700            check.error(format!("{}: ignoring trailing whitespace unnecessarily", file.display()));701        }702        if let Directive::Ignore(false) = skip_trailing_newlines {703            check.error(format!("{}: ignoring trailing newlines unnecessarily", file.display()));704        }705        if let Directive::Ignore(false) = skip_leading_newlines {706            check.error(format!("{}: ignoring leading newlines unnecessarily", file.display()));707        }708        if let Directive::Ignore(false) = skip_copyright {709            check.error(format!("{}: ignoring copyright unnecessarily", file.display()));710        }711        // We deliberately do not warn about these being unnecessary,712        // that would just lead to annoying churn.713        let _unused = skip_line_length;714        let _unused = skip_file_length;715    });716}

Code quality findings 15

Critical: Use of 'unsafe' keyword bypasses Rust's safety guarantees. Requires careful auditing, clear justification (FFI, specific optimizations), and minimal scope.
error safety unsafe-block
if trimmed.contains("unsafe {")
Warning: Direct indexing (e.g., `vec[i]`, `slice[i]`) panics on out-of-bounds access. Prefer using `.get(index)` or `.get_mut(index)` which return Option<&T>/Option<&mut T>.
warning correctness unchecked-indexing
consts: &'a [u32],
Warning: Direct indexing (e.g., `vec[i]`, `slice[i]`) panics on out-of-bounds access. Prefer using `.get(index)` or `.get_mut(index)` which return Option<&T>/Option<&mut T>.
warning correctness unchecked-indexing
if (i >> pos) & 1 == 1 { letter_digit[&c] } else { c }
Warning: '.unwrap()' will panic on None/Err variants. Prefer using pattern matching (match, if let), combinators (map, and_then), or the '?' operator for robust error handling.
warning correctness unwrap-usage
.unwrap()
Warning: Direct indexing (e.g., `vec[i]`, `slice[i]`) panics on out-of-bounds access. Prefer using `.get(index)` or `.get_mut(index)` which return Option<&T>/Option<&mut T>.
warning correctness unchecked-indexing
return [Directive::Deny; N];
Warning: '.unwrap()' will panic on None/Err variants. Prefer using pattern matching (match, if let), combinators (map, and_then), or the '?' operator for robust error handling.
warning correctness unwrap-usage
.unwrap();
Warning: '.unwrap()' will panic on None/Err variants. Prefer using pattern matching (match, if let), combinators (map, and_then), or the '?' operator for robust error handling.
warning correctness unwrap-usage
let filename = file.file_name().unwrap().to_string_lossy();
Warning: '.unwrap()' will panic on None/Err variants. Prefer using pattern matching (match, if let), combinators (map, and_then), or the '?' operator for robust error handling.
warning correctness unwrap-usage
let extension = file.extension().unwrap().to_string_lossy();
Warning: Direct indexing (e.g., `vec[i]`, `slice[i]`) panics on out-of-bounds access. Prefer using `.get(index)` or `.get_mut(index)` which return Option<&T>/Option<&mut T>.
warning correctness unchecked-indexing
let [
Warning: '.unwrap()' will panic on None/Err variants. Prefer using pattern matching (match, if let), combinators (map, and_then), or the '?' operator for robust error handling.
warning correctness unwrap-usage
|| file.file_stem().unwrap() == "tests";
Warning: '.unwrap()' will panic on None/Err variants. Prefer using pattern matching (match, if let), combinators (map, and_then), or the '?' operator for robust error handling.
warning correctness unwrap-usage
is_test && file.parent().unwrap().ends_with(this_file.with_extension(""));
Warning: '.expect()' will panic with a custom message on None/Err. While better than unwrap() for debugging, prefer non-panicking error handling in production code (match, if let, ?).
warning correctness expect-usage
trimmed.split_once("doc").expect("failed to find `doc` attribute");
Info: Usage of `#[allow(...)]` suppresses compiler lints. Ensure the allowance is justified, well-scoped, and ideally temporary. Overuse can hide potential issues.
info maintainability allow-lint
#[allow(non_camel_case_types)]
Info: Wildcard imports (`use some::path::*;`) can obscure the origin of names and lead to conflicts. Prefer importing specific items explicitly.
info maintainability wildcard-import
use self::LIUState::*;
Info: Ensure 'match' statements are exhaustive. If matching on enums, consider adding a wildcard arm `_ => {}` only if necessary and intentional, as it suppresses warnings about unhandled variants.
info correctness match-wildcard
match extension {

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.