Skip to content

rustc_deprecated suggestions don't always cargo fix correctly #84637

Closed
@carols10cents

Description

@carols10cents

Rust version: stable 1.51.0

Given the following code that uses the std::str::trim_left function that is marked rustc_deprecated:

fn main() {
    let foo = str::trim_left("   aoeu");
    
    let bar = "   aoeu".trim_left();
}

The current output is:

warning: use of deprecated associated function `core::str::<impl str>::trim_left`: superseded by `trim_start`
 --> src/main.rs:2:15
  |
2 |     let foo = str::trim_left("   aoeu");
  |               ^^^^^^^^^^^^^^ help: replace the use of the deprecated associated function: `trim_start`
  |
  = note: `#[warn(deprecated)]` on by default

warning: use of deprecated associated function `core::str::<impl str>::trim_left`: superseded by `trim_start`
 --> src/main.rs:4:25
  |
4 |     let bar = "   aoeu".trim_left();
  |                         ^^^^^^^^^ help: replace the use of the deprecated associated function: `trim_start`

If I run cargo fix on this example, I get this output:

warning: failed to automatically apply fixes suggested by rustc to crate `foo`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0425]: cannot find function `trim_start` in this scope
 --> src/main.rs:2:16
  |
2 |     let _foo = trim_start("   aoeu");
  |                ^^^^^^^^^^ not found in this scope

The suggestion from the rustc_deprecated attribute applied correctly to the usage on line 4, but it replaced str::trim_left with just trim_start on line 2. I expected it to change this line to let foo = str::trim_start(" aoeu");.

I'm not sure if the solution is to change the ^^^^^^ span marking to the leaf item that's deprecated. It seems like perhaps if the warning instead was:

2 |     let foo = str::trim_left("   aoeu");
  |                    ^^^^^^^^^ help: replace the use of the deprecated associated function: `trim_start`

then perhaps only the trim_left in str::trim_left would be replaced by rustfix? If so, I'd like to try my hand at fixing this. I've read some rustc_deprecated related issues, however, and I'm getting a strong here-be-dragons vibe from them, so I wanted to check with someone more knowledgeable about if I'm on the right track before the yaks eat me.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-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