Skip to content

ptr_arg lint wrongly suggests replacing &mut Vec<_> parameters with &mut [_] if the function body uses the Vec API #8482

Closed
@sdroege

Description

@sdroege

Summary

The function in question takes mutable Vecs as parameters and modifies them.

Lint Name

ptr_arg

Reproducer

I tried this code:

pub fn generate_reexports(
    env: &Env,
    analysis: &analysis::object::Info,
    module_name: &str,
    contents: &mut Vec<String>,
    traits: &mut Vec<String>,
    builders: &mut Vec<String>,
) {
    let mut cfgs: Vec<String> = Vec::new();
    if let Some(cfg) = general::cfg_condition_string(analysis.cfg_condition.as_ref(), false, 0) {
        cfgs.push(cfg);
    }
    if let Some(cfg) = general::version_condition_string(env, None, analysis.version, false, 0) {
        cfgs.push(cfg);
    }
    if let Some(cfg) = general::cfg_deprecated_string(
        env,
        Some(analysis.type_id),
        analysis.deprecated_version,
        false,
        0,
    ) {
        cfgs.push(cfg);
    }

    contents.push("".to_owned());
    contents.extend_from_slice(&cfgs);
    contents.push(format!("mod {};", module_name));
    contents.extend_from_slice(&cfgs);

    contents.push(format!(
        "{} use self::{}::{};",
        analysis.visibility.export_visibility(),
        module_name,
        analysis.name,
    ));

    if analysis.need_generate_trait() {
        for cfg in &cfgs {
            traits.push(format!("\t{}", cfg));
        }
        traits.push(format!(
            "\tpub use super::{}::{};",
            module_name, analysis.trait_name
        ));
    }

    if has_builder_properties(&analysis.builder_properties) {
        for cfg in &cfgs {
            builders.push(format!("\t{}", cfg));
        }
        builders.push(format!(
            "\tpub use super::{}::{}Builder;",
            module_name, analysis.name
        ));
    }
}

I saw this happen:

warning: writing `&mut Vec` instead of `&mut [_]` involves a new object where a slice will do
   --> src/codegen/object.rs:589:13
    |
589 |     traits: &mut Vec<String>,
    |             ^^^^^^^^^^^^^^^^ help: change this to: `&mut [String]`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_arg

warning: writing `&mut Vec` instead of `&mut [_]` involves a new object where a slice will do
   --> src/codegen/object.rs:590:15
    |
590 |     builders: &mut Vec<String>,
    |               ^^^^^^^^^^^^^^^^ help: change this to: `&mut [String]`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_arg

I expected to see this happen: Lint doesn't trigger

Version

rustc 1.60.0-beta.1 (0a4f984a8 2022-02-22)
binary: rustc
commit-hash: 0a4f984a87c7ba6c74ec3e78442fec955a419e32
commit-date: 2022-02-22
host: x86_64-unknown-linux-gnu
release: 1.60.0-beta.1
LLVM version: 14.0.0

Additional Labels

@rustbot label +I-suggestion-causes-error

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions