Skip to content

[rust-guard] Reduce redundant closures and unnecessary Vec ownership in integrity helpers #13844

Description

@github-actions

Improvement 1: Replace redundant closures with method references in serde_json::Value extraction

Category: Type Safety
File(s): labels/helpers.rs, labels/backend.rs, labels/response_items.rs, labels/response_paths.rs
Effort: Small (< 15 min)
Risk: Low

Problem

cargo clippy -- -W clippy::pedantic flags 11 sites using .and_then(|v| v.as_bool()) / .and_then(|v| v.as_u64()) / .map(|value| value.trim()) where the closure is a redundant wrapper around a method call. Clippy's redundant_closure_for_method_calls lint recommends passing the method itself.

Sites:

  • labels/helpers.rs:50 — .and_then(|v| v.as_u64())
  • labels/helpers.rs:73 — .and_then(|v| v.as_u64())
  • labels/helpers.rs:97 — .and_then(|v| v.as_u64())
  • labels/helpers.rs:341 — .map(|value| value.trim())
  • labels/helpers.rs:1150 — .and_then(|v| v.as_bool())
  • labels/helpers.rs:1753 — .and_then(|v| v.as_bool())
  • labels/backend.rs:692 — .and_then(|v| v.as_bool())
  • labels/backend.rs:1569 — .and_then(|v| v.as_bool())
  • labels/response_items.rs:73 — .and_then(|v| v.as_bool())
  • labels/response_paths.rs:97 — .and_then(|v| v.as_bool())

Suggested Change

Replace each closure with the bare method reference.

Before

// labels/helpers.rs:50
if let Some(n) = item.get(field_names::NUMBER).and_then(|v| v.as_u64()) {
// labels/helpers.rs:341
.map(|value| value.trim())

After

// labels/helpers.rs:50
if let Some(n) = item.get(field_names::NUMBER).and_then(Value::as_u64) {
// labels/helpers.rs:341
.map(str::trim)

Apply the same and_then(Value::as_bool) / and_then(Value::as_u64) substitution at the other 9 sites listed above (note serde_json::Value is already in scope in all four files).

Why This Matters

Removes 11 unnecessary closure allocations flagged by clippy::pedantic, clarifies intent (the code is just delegating to an existing method), and is a pure syntactic simplification with zero behavior change — trivially verified by cargo clippy and the existing test suite.


Improvement 2: Take integrity Vec<String> arguments by reference in cap_integrity/max_integrity

Category: Type Safety / Performance
File(s): labels/helpers.rs
Effort: Small (< 15 min)
Risk: Low

Problem

cap_integrity (line 654) and max_integrity (line 1645) both take current/cap/candidate as owned Vec<String>, but each function only ever reads the vectors via integrity_rank_normalized(&normalized_scope, &current) — it never mutates or moves them. Clippy's needless_pass_by_value pedantic lint flags this. Because the parameters are owned, two test call sites are forced to add avoidable .clone() calls just to keep using the value afterward for an assertion:

  • labels/helpers.rs:3413 — cap_integrity(scope, current.clone(), cap, &ctx) (clones current only so it can be compared against result afterward)
  • labels/helpers.rs:3426 — max_integrity(scope, current, candidate.clone(), &ctx) (clones candidate for the same reason)

Suggested Change

Change both function signatures to accept &[String] instead of Vec<String>. Update the ~13 call sites across labels/helpers.rs and labels/tool_rules.rs to pass references (e.g. &integrity, &cap) instead of owned vectors — most call sites already own a local Vec<String> binding, so this is a & prefix change, not a restructure.

Before

fn cap_integrity(
    scope: &str,
    current: Vec<String>,
    cap: Vec<String>,
    ctx: &PolicyContext,
) -> Vec<String> {
    let normalized_scope = normalize_scope(scope, ctx);
    let current_rank = integrity_rank_normalized(&normalized_scope, &current);
    let cap_rank = integrity_rank_normalized(&normalized_scope, &cap);
    build_integrity_labels(
        &normalized_scope,
        current_rank.min(cap_rank).saturating_sub(1) as usize,
    )
}
// test call site — clone only needed because cap_integrity takes ownership
let result = cap_integrity(scope, current.clone(), cap, &ctx);

After

fn cap_integrity(
    scope: &str,
    current: &[String],
    cap: &[String],
    ctx: &PolicyContext,
) -> Vec<String> {
    let normalized_scope = normalize_scope(scope, ctx);
    let current_rank = integrity_rank_normalized(&normalized_scope, current);
    let cap_rank = integrity_rank_normalized(&normalized_scope, cap);
    build_integrity_labels(
        &normalized_scope,
        current_rank.min(cap_rank).saturating_sub(1) as usize,
    )
}
// test call site — no clone needed, current is still owned by the caller
let result = cap_integrity(scope, &current, &cap, &ctx);

Apply the equivalent signature change to max_integrity, and update all callers (e.g. labels/tool_rules.rs:199, :212, :402-431; labels/helpers.rs:471, 550, 884, 913, 927, 1849, 1980-2222) to pass &integrity/&floor/&cap etc.

Why This Matters

Eliminates the needless_pass_by_value clippy warning at 4 parameter sites, removes 2 test-only .clone() calls that exist purely as a workaround for the ownership requirement, and avoids moving/copying Vec<String> at every one of the ~15 call sites in hot label-computation paths (each call currently transfers ownership of a heap-allocated vector it didn't need to consume).


Codebase Health Summary

  • Total Rust files: 9
  • Total lines: 21,807
  • Areas analyzed: lib.rs, tools.rs, labels/mod.rs, labels/backend.rs, labels/constants.rs, labels/helpers.rs, labels/response_items.rs, labels/response_paths.rs, labels/tool_rules.rs
  • Areas with no further improvements found this run: labels/constants.rs, tools.rs (well-organized, no new raw-literal duplication found beyond prior extractions)

Note: cargo clippy --lib -- -W clippy::pedantic also surfaces ~90 uninlined_format_args and ~60 doc-backtick warnings, but these are high-volume/low-value style nits better handled by a bulk cargo clippy --fix pass rather than a manual PR, so they were excluded from this report in favor of the two concrete, hand-verified improvements above.


Generated by Rust Guard Improver • Run: 36117626642

Warning

Firewall blocked 2 domains

The following domains were blocked by the firewall during workflow execution:

  • index.crates.io
  • static.crates.io

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "index.crates.io"
    - "static.crates.io"

See Network Configuration for more information.

Generated by Rust Guard Improver · copilot · auto · 112.7 AIC · ⊞ 11.3K · ◷

  • expires on Oct 2, 2026, 9:26 AM UTC

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions