Repository: astral-sh/ruff
File: crates/ruff_linter/src/rules/ruff/rules/invalid_rule_code.rs (Rust)

Code before:
        if all_valid {
            return;
        }

        let (valid_codes, invalid_codes): (Vec<_>, Vec<_>) = codes
            .iter()
            .partition(|&code| code_is_valid(code.as_str(), external));

        if valid_codes.is_empty() {
            all_codes_invalid_diagnostic(codes, invalid_codes, locator, context);
        } else {
            for invalid_code in invalid_codes {
                some_codes_are_invalid_diagnostic(codes, invalid_code, locator, context);
            }
        }
    };

    for line in file_noqa_directives.lines() {
        if let Directive::Codes(codes) = &line.parsed_file_exemption {
            check_codes(codes);
        }
    }
    for line in noqa_directives.lines() {
        if let Directive::Codes(codes) = &line.directive {
            check_codes(codes);
        }
    }
}

pub(crate) fn code_is_valid(code: &str, external: &[String]) -> bool {
    Rule::from_code(get_redirect_target(code).unwrap_or(code)).is_ok()
        || external.iter().any(|ext| code.starts_with(ext))
}

fn all_codes_invalid_diagnostic(
    directive: &Codes<'_>,
    invalid_codes: Vec<&Code<'_>>,
    locator: &Locator,
    context: &LintContext,
) {
    let mut diagnostic = context.report_custom_diagnostic(
        InvalidRuleCode {
            rule_code: invalid_codes
                .into_iter()
                .map(Code::as_str)
                .collect::<Vec<_>>()
                .join(", "),
            kind: InvalidRuleCodeKind::Noqa,
            whole_comment: true,
        },
        directive.range(),

Review comment (suggestion):
Since it looks like these have the same type in both cases, instead of a closure, we could just create a chained iterator with something like:

```rust
    for directive in file_noqa_directives
        .lines()
        .iter()
        .map(|line| &line.parsed_file_exemption)
        .chain(noqa_directives.lines().iter().map(|line| &line.directive))
```

and iterate over that. The `.lines().iter()` is admittedly a bit noisy, but it works.

Code after:

        if all_valid {
            return;
        }

        let (valid_codes, invalid_codes): (Vec<_>, Vec<_>) = codes
            .iter()
            .partition(|&code| code_is_valid(code.as_str(), external));

        if valid_codes.is_empty() {
            all_codes_invalid_diagnostic(codes, invalid_codes, locator, context);
        } else {
            for invalid_code in invalid_codes {
                some_codes_are_invalid_diagnostic(codes, invalid_code, locator, context);
            }
        }
    };

    if is_file_level_invalid_rule_code_enabled(context.settings()) {
        for line in file_noqa_directives.lines() {
            if let Directive::Codes(codes) = &line.parsed_file_exemption {
                check_codes(codes);
            }
        }
    }
    for line in noqa_directives.lines() {
        if let Directive::Codes(codes) = &line.directive {
            check_codes(codes);
        }
    }
}

pub(crate) fn code_is_valid(code: &str, external: &[String]) -> bool {
    Rule::from_code(get_redirect_target(code).unwrap_or(code)).is_ok()
        || external.iter().any(|ext| code.starts_with(ext))
}

fn all_codes_invalid_diagnostic(
    directive: &Codes<'_>,
    invalid_codes: Vec<&Code<'_>>,
    locator: &Locator,
    context: &LintContext,
) {
    let mut diagnostic = context.report_custom_diagnostic(
        InvalidRuleCode {
            rule_code: invalid_codes
                .into_iter()
                .map(Code::as_str)
                .collect::<Vec<_>>()
                .join(", "),
            kind: InvalidRuleCodeKind::Noqa,