Skip to content

Suggest removal of .as_str() when called on a &str #62642

Closed
@estebank

Description

@estebank

Given

fn make_lipographic(banned: char, line: &str) -> String {
    line.as_str().chars().filter(|&c| c != banned).collect()
}

fn main() {
    let passage = String::from("If Youth, throughout all history, had had a champion to stand up for it");
    assert_eq!(make_lipographic('e', &passage), passage);
}

we currently emit

error[E0599]: no method named `as_str` found for type `&str` in the current scope
 --> src/main.rs:2:10
  |
2 |     line.as_str().chars().filter(|&c| c != banned).collect()
  |          ^^^^^^ help: there is a method with a similar name: `as_ptr`

This is a possible simple WIP mistake when changing some binding from String to &str that we could preempt and avoid a pretty bad suggestion.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.P-lowLow priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions