Skip to content

[rust-guard] Rust Guard: Remove dead is_bot function + eliminate allocation in check_file_secrecy #3270

@github-actions

Description

@github-actions

🦀 Rust Guard Improvement Report

Improvement 1: Remove Dead is_bot Function

Category: Dead Code
File(s): guards/github-guard/rust-guard/src/labels/helpers.rs, guards/github-guard/rust-guard/src/labels/mod.rs
Effort: Small (< 15 min)
Risk: Low

Problem

is_bot (helpers.rs:1331) is decorated with #[allow(dead_code)] and has zero call sites in the entire codebase. It is re-exported via pub use helpers::is_bot in mod.rs:50, but since this is a cdylib (WASM), no external Rust caller can consume that export. The function compiles into the binary without being called, adding unnecessary size to the WASM artifact.

$ grep -rn "is_bot(" src/
src/labels/helpers.rs:1331:pub fn is_bot(username: &str) -> bool {
# ← only the definition; zero call sites

Suggested Change

Remove the is_bot function from helpers.rs and remove it from the pub use list in mod.rs.

Before

helpers.rs (lines 1325–1343):

/// Check if a user appears to be a bot (broad detection).
///
/// This is a broader check that includes third-party bots.
/// For integrity elevation, use is_trusted_first_party_bot() instead.
#[allow(dead_code)]
pub fn is_bot(username: &str) -> bool {
    let lower = username.to_lowercase();
    lower.ends_with("[bot]")
        || lower.ends_with("-bot")
        || lower == "dependabot"
        || lower == "renovate"
        || lower == "github-actions"
        || lower == "copilot"
}

mod.rs (line 50):

pub use helpers::{
    has_author_association, is_blocked_user, is_bot, is_graphql_wrapper, is_mcp_text_wrapper,
    // ...
};

After

helpers.rs: delete the is_bot function entirely.

mod.rs:

pub use helpers::{
    has_author_association, is_blocked_user, is_graphql_wrapper, is_mcp_text_wrapper,
    // ...  (is_bot removed)
};

Also update labels/README.md line 32 which mentions is_bot in the helper list.

Why This Matters

  • Removes dead code that silently inflates the WASM binary size.
  • Eliminates the #[allow(dead_code)] suppression — a code smell signalling the function was known-unused at the time of writing.
  • If bot-detection is ever needed, it can be re-added with an actual call site.

Improvement 2: Eliminate format! Allocation Inside check_file_secrecy Loop

Category: Performance (WASM-specific)
File(s): guards/github-guard/rust-guard/src/labels/tool_rules.rs
Effort: Small (< 15 min)
Risk: Low

Problem

check_file_secrecy (tool_rules.rs:685) allocates a new String for every pattern in SENSITIVE_FILE_PATTERNS (9 entries) on every invocation:

for pattern in SENSITIVE_FILE_PATTERNS {
    if path_lower.ends_with(pattern) || path_lower.contains(&format!("/{}", pattern)) {

format!("/{}", pattern) creates a heap-allocated String on each loop iteration. For a WASM binary that processes many file paths, this is unnecessary allocation pressure.

Suggested Change

Replace contains(&format!("/{}", pattern)) with split('/').any(|seg| seg == *pattern). This checks path components without any allocation and is semantically equivalent for well-formed file paths.

Equivalence proof:

  • path.contains("/X") is true iff some path component equals X (preceded by /)
  • path.split('/').any(|s| s == X) is true iff some path component equals X

Both conditions are identical for standard POSIX-style file paths.

Before

// Check for sensitive file extensions/names
for pattern in SENSITIVE_FILE_PATTERNS {
    if path_lower.ends_with(pattern) || path_lower.contains(&format!("/{}", pattern)) {
        return policy_private_scope_label(owner, repo, repo_id, ctx);
    }
}

After

// Check for sensitive file extensions/names
for pattern in SENSITIVE_FILE_PATTERNS {
    if path_lower.ends_with(pattern) || path_lower.split('/').any(|seg| seg == *pattern) {
        return policy_private_scope_label(owner, repo, repo_id, ctx);
    }
}

Why This Matters

  • Eliminates up to 9 heap allocations per check_file_secrecy call (one per pattern).
  • In WASM environments, every allocation goes through the Wasm linear-memory allocator. Reducing allocations in file-path checking hot paths directly reduces memory pressure and improves throughput.
  • The change is fully behavior-preserving and trivially verifiable by inspection.

Codebase Health Summary

  • Total Rust files: 10
  • Total lines: 10,948
  • Areas analyzed: lib.rs, tools.rs, labels/mod.rs, labels/helpers.rs, labels/backend.rs, labels/constants.rs, labels/tool_rules.rs, labels/response_items.rs, labels/response_paths.rs
  • Areas with no further improvements: none yet (first run)

Generated by Rust Guard Improver • Run: §24026828906

Generated by Rust Guard Improver · ● 1.5M ·

  • expires on Apr 13, 2026, 9:43 AM UTC

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions