Skip to content

New lint: manual_inspect #16777

@profetia

Description

@profetia

What it does

For the following case:

if let Some(legacy_path) = legacy_bin_path(package_root, &bin.name(), has_lib) {
    warnings.push(format!(
        "path `{}` was erroneously implicitly accepted for binary `{}`,\n\
         please set bin.path in Cargo.toml",
        legacy_path.display(),
        bin.name()
    ));
    Some(legacy_path)
} else {
    None
}

It is better to use Option::inspect.

Advantage

  • Better readability

Drawbacks

Cannot think of so far.

Example

if let Some(legacy_path) = legacy_bin_path(package_root, &bin.name(), has_lib) {
    warnings.push(format!(
        "path `{}` was erroneously implicitly accepted for binary `{}`,\n\
         please set bin.path in Cargo.toml",
        legacy_path.display(),
        bin.name()
    ));
    Some(legacy_path)
} else {
    None
}

Could be written as:

legacy_bin_path(package_root, &bin.name(), has_lib)
    .inspect(|legacy_path| {
        warnings.push(format!(
            "path `{}` was erroneously implicitly accepted for binary `{}`,\n\
             please set bin.path in Cargo.toml",
            legacy_path.display(),
            bin.name()
        ));
    })

Comparison with existing lints

The given example will be covered by clippy::manual_map if #16775 is merged, but using Option::inspect would be better.

Additional Context

One real world example: https://docs.rs/cargo/0.64.0/src/cargo/util/toml/targets.rs.html#325

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: Enhancement of lints, like adding more cases or adding help messages

    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