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

Code before:
/// import pyghmi
/// ```
///
/// ## References
/// - [Buffer Overflow Issue](https://github.com/pycrypto/pycrypto/issues/176)
#[derive(ViolationMetadata)]
#[violation_metadata(preview_since = "v0.1.12")]
pub(crate) struct SuspiciousPyghmiImport;

impl Violation for SuspiciousPyghmiImport {
    #[derive_message_formats]
    fn message(&self) -> String {
        "An IPMI-related module is being imported. Prefer an encrypted protocol over IPMI."
            .to_string()
    }
}

/// S401, S402, S403, S404, S405, S406, S407, S408, S409, S410, S411, S412, S413, S415
pub(crate) fn suspicious_imports(checker: &Checker, stmt: &Stmt) {
    // Skip stub files.
    if checker.source_type.is_stub() {
        return;
    }

    // Imports inside `if TYPE_CHECKING:` are not executed at runtime.
    let is_type_checking_block = checker.semantic().in_type_checking_block();

    match stmt {
        Stmt::Import(ast::StmtImport { names, .. }) => {
            for name in names {
                match name.name.as_str() {
                    "telnetlib" => {
                        checker.report_diagnostic_if_enabled(SuspiciousTelnetlibImport, name.range);
                    }
                    "ftplib" => {
                        checker.report_diagnostic_if_enabled(SuspiciousFtplibImport, name.range);
                    }
                    "pickle" | "cPickle" | "dill" | "shelve" => {
                        checker.report_diagnostic_if_enabled(SuspiciousPickleImport, name.range);
                    }
                    "subprocess" => {
                        checker
                            .report_diagnostic_if_enabled(SuspiciousSubprocessImport, name.range);
                    }
                    "xml.etree.cElementTree" | "xml.etree.ElementTree" => {
                        checker.report_diagnostic_if_enabled(SuspiciousXmlEtreeImport, name.range);
                    }
                    "xml.sax" => {
                        checker.report_diagnostic_if_enabled(SuspiciousXmlSaxImport, name.range);
                    }
                    "xml.dom.expatbuilder" => {

Review comment (suggestion):
I think it would make sense to return right here for all of these rules. The same argument about `TYPE_CHECKING` seems to apply to all of the rules  that only check imports, and this would avoid all the separate checks and `continue`s.

Code after:
/// import pyghmi
/// ```
///
/// ## References
/// - [Buffer Overflow Issue](https://github.com/pycrypto/pycrypto/issues/176)
#[derive(ViolationMetadata)]
#[violation_metadata(preview_since = "v0.1.12")]
pub(crate) struct SuspiciousPyghmiImport;

impl Violation for SuspiciousPyghmiImport {
    #[derive_message_formats]
    fn message(&self) -> String {
        "An IPMI-related module is being imported. Prefer an encrypted protocol over IPMI."
            .to_string()
    }
}

/// S401, S402, S403, S404, S405, S406, S407, S408, S409, S410, S411, S412, S413, S415
pub(crate) fn suspicious_imports(checker: &Checker, stmt: &Stmt) {
    // Skip stub files.
    if checker.source_type.is_stub() {
        return;
    }

    // Imports inside `if TYPE_CHECKING:` are not executed at runtime.
    if checker.semantic().in_type_checking_block() {
        return;
    }

    match stmt {
        Stmt::Import(ast::StmtImport { names, .. }) => {
            for name in names {
                match name.name.as_str() {
                    "telnetlib" => {
                        checker.report_diagnostic_if_enabled(SuspiciousTelnetlibImport, name.range);
                    }
                    "ftplib" => {
                        checker.report_diagnostic_if_enabled(SuspiciousFtplibImport, name.range);
                    }
                    "pickle" | "cPickle" | "dill" | "shelve" => {
                        checker.report_diagnostic_if_enabled(SuspiciousPickleImport, name.range);
                    }
                    "subprocess" => {
                        checker
                            .report_diagnostic_if_enabled(SuspiciousSubprocessImport, name.range);
                    }
                    "xml.etree.cElementTree" | "xml.etree.ElementTree" => {
                        checker.report_diagnostic_if_enabled(SuspiciousXmlEtreeImport, name.range);
                    }
                    "xml.sax" => {
                        checker.report_diagnostic_if_enabled(SuspiciousXmlSaxImport, name.range);