Skip to content

useless_asref lint produces incorrect fix suggestion that causes type error with Cow::Borrowed #16098

@azjezz

Description

@azjezz

Summary

When running cargo +nightly clippy --fix, the useless_asref lint incorrectly suggests removing .as_ref() from code that maps &&str to Cow::Borrowed(&str), resulting in a type error after the automatic fix is applied.

Lint Name

clippy::useless_asref

Reproducer

Clone and checkout the repository

git clone git@github.com:carthage-software/mago.git
cd mago/mago
git checkout 0de9868  # commit where the issue occurs

Run clippy with automatic fixes

cargo +nightly clippy --workspace --all-targets --all-features --fix --allow-dirty --allow-staged

The command will fail with the following error after attempting to apply the automatic fix:

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

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

  * crates/orchestrator/src/lib.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report!

The following errors were reported:
error[E0277]: a value of type `std::vec::Vec<std::borrow::Cow<'_, str>>` cannot be built from an iterator over elements of type `std::borrow::Cow<'_, &str>`
    --> crates/orchestrator/src/lib.rs:186:81
     |
 186 |             extensions: self.config.extensions.iter().map(|s| Cow::Borrowed(s)).collect(),
     |                                                                                 ^^^^^^^ value of type `std::vec::Vec<std::borrow::Cow<'_, str>>` cannot be built from `std::iter::Iterator<Item=std::borrow::Cow<'_, &str>>`
     |
     = help: for that trait implementation, expected `str`, found `&str`

Original Warning

Before the fix is applied, clippy shows:

warning: this call to `as_ref` does nothing
   --> crates/orchestrator/src/lib.rs:186:77
    |
186 |             extensions: self.config.extensions.iter().map(|s| Cow::Borrowed(s.as_ref())).collect(),
    |                                                                             ^^^^^^^^^^ help: try: `s`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_asref

Expected Behavior

Either:

  1. The lint should suggest replacing s.as_ref() with *s (the correct fix), or
  2. The lint should not trigger on this code (since .as_ref() serves a purpose)

Actual Behavior

The lint triggers and suggests removing .as_ref() entirely, then applies the fix automatically when using --fix, causing the code to fail compilation.

Correct Fix

The correct fix is to replace s.as_ref() with *s:

// Before (triggers clippy warning)
extensions: self.config.extensions.iter().map(|s| Cow::Borrowed(s.as_ref())).collect(),

// Clippy's suggested fix (causes compilation error)
extensions: self.config.extensions.iter().map(|s| Cow::Borrowed(s)).collect(),

// Correct fix (compiles and satisfies clippy)
extensions: self.config.extensions.iter().map(|s| Cow::Borrowed(*s)).collect(),

Full Output

    Checking mago-orchestrator v1.0.0-rc.2 (/Users/azjezz/mago/mago/crates/orchestrator)
    Checking mago-guard v1.0.0-rc.2 (/Users/azjezz/mago/mago/crates/guard)
    Checking mago-linter v1.0.0-rc.2 (/Users/azjezz/mago/mago/crates/linter)
    Checking mago-formatter v1.0.0-rc.2 (/Users/azjezz/mago/mago/crates/formatter)
    Checking mago-analyzer v1.0.0-rc.2 (/Users/azjezz/mago/mago/crates/analyzer)
warning: failed to automatically apply fixes suggested by rustc to crate `mago_orchestrator`

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

  * crates/orchestrator/src/lib.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-clippy/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[E0277]: a value of type `std::vec::Vec<std::borrow::Cow<'_, str>>` cannot be built from an iterator over elements of type `std::borrow::Cow<'_, &str>`
    --> crates/orchestrator/src/lib.rs:186:81
     |
 186 |             extensions: self.config.extensions.iter().map(|s| Cow::Borrowed(s)).collect(),
     |                                                                                 ^^^^^^^ value of type `std::vec::Vec<std::borrow::Cow<'_, str>>` cannot be built from `std::iter::Iterator<Item=std::borrow::Cow<'_, &str>>`
     |
help: the trait `FromIterator<std::borrow::Cow<'_, &_>>` is not implemented for `std::vec::Vec<std::borrow::Cow<'_, str>>`
      but trait `FromIterator<std::borrow::Cow<'_, _>>` is implemented for it
    --> /Users/azjezz/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:3670:1
     |
3670 | impl<T> FromIterator<T> for Vec<T> {
     | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     = help: for that trait implementation, expected `str`, found `&str`
note: the method call chain might not have had the expected associated types
    --> crates/orchestrator/src/lib.rs:186:55
     |
 186 |             extensions: self.config.extensions.iter().map(|s| Cow::Borrowed(s)).collect(),
     |                         ---------------------- ------ ^^^^^^^^^^^^^^^^^^^^^^^^^ `Iterator::Item` changed to `Cow<'_, &str>` here
     |                         |                      |
     |                         |                      `Iterator::Item` is `&&str` here
     |                         this expression has type `Vec<&str>`
note: required by a bound in `std::iter::Iterator::collect`
    --> /Users/azjezz/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:2015:19
     |
2015 |     fn collect<B: FromIterator<Self::Item>>(self) -> B
     |                   ^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Iterator::collect`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.
Original diagnostics will follow.

warning: this call to `as_ref` does nothing
   --> crates/orchestrator/src/lib.rs:186:77
    |
186 |             extensions: self.config.extensions.iter().map(|s| Cow::Borrowed(s.as_ref())).collect(),
    |                                                                             ^^^^^^^^^^ help: try: `s`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_asref
    = note: `#[warn(clippy::useless_asref)]` on by default

warning: `mago-orchestrator` (lib) generated 1 warning (run `cargo clippy --fix --lib -p mago-orchestrator` to apply 1 suggestion)
    Checking mago v1.0.0-rc.2 (/Users/azjezz/mago/mago)
    Checking mago-wasm v1.0.0-rc.2 (/Users/azjezz/mago/mago/crates/wasm)
warning: failed to automatically apply fixes suggested by rustc to crate `mago_orchestrator`

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

  * crates/orchestrator/src/lib.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-clippy/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[E0277]: a value of type `std::vec::Vec<std::borrow::Cow<'_, str>>` cannot be built from an iterator over elements of type `std::borrow::Cow<'_, &str>`
    --> crates/orchestrator/src/lib.rs:186:81
     |
 186 |             extensions: self.config.extensions.iter().map(|s| Cow::Borrowed(s)).collect(),
     |                                                                                 ^^^^^^^ value of type `std::vec::Vec<std::borrow::Cow<'_, str>>` cannot be built from `std::iter::Iterator<Item=std::borrow::Cow<'_, &str>>`
     |
help: the trait `FromIterator<std::borrow::Cow<'_, &_>>` is not implemented for `std::vec::Vec<std::borrow::Cow<'_, str>>`
      but trait `FromIterator<std::borrow::Cow<'_, _>>` is implemented for it
    --> /Users/azjezz/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:3670:1
     |
3670 | impl<T> FromIterator<T> for Vec<T> {
     | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     = help: for that trait implementation, expected `str`, found `&str`
note: the method call chain might not have had the expected associated types
    --> crates/orchestrator/src/lib.rs:186:55
     |
 186 |             extensions: self.config.extensions.iter().map(|s| Cow::Borrowed(s)).collect(),
     |                         ---------------------- ------ ^^^^^^^^^^^^^^^^^^^^^^^^^ `Iterator::Item` changed to `Cow<'_, &str>` here
     |                         |                      |
     |                         |                      `Iterator::Item` is `&&str` here
     |                         this expression has type `Vec<&str>`
note: required by a bound in `std::iter::Iterator::collect`
    --> /Users/azjezz/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:2015:19
     |
2015 |     fn collect<B: FromIterator<Self::Item>>(self) -> B
     |                   ^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Iterator::collect`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.
Original diagnostics will follow.

warning: `mago-orchestrator` (lib test) generated 1 warning (1 duplicate)
    Checking mago-prelude v1.0.0-rc.2 (/Users/azjezz/mago/mago/crates/prelude)
    Checking mago-casing v1.0.0-rc.2 (/Users/azjezz/mago/mago/crates/casing)
    Checking mago-algebra v1.0.0-rc.2 (/Users/azjezz/mago/mago/crates/algebra)
    Checking mago-fingerprint v1.0.0-rc.2 (/Users/azjezz/mago/mago/crates/fingerprint)
    Checking mago-codex v1.0.0-rc.2 (/Users/azjezz/mago/mago/crates/codex)
    Checking mago-semantics v1.0.0-rc.2 (/Users/azjezz/mago/mago/crates/semantics)
    Checking mago-atom v1.0.0-rc.2 (/Users/azjezz/mago/mago/crates/atom)
    Checking mago-syntax v1.0.0-rc.2 (/Users/azjezz/mago/mago/crates/syntax)
    Checking mago-names v1.0.0-rc.2 (/Users/azjezz/mago/mago/crates/names)
    Checking mago-docblock v1.0.0-rc.2 (/Users/azjezz/mago/mago/crates/docblock)
    Checking mago-fixer v1.0.0-rc.2 (/Users/azjezz/mago/mago/crates/fixer)
    Checking mago-collector v1.0.0-rc.2 (/Users/azjezz/mago/mago/crates/collector)
    Checking mago-type-syntax v1.0.0-rc.2 (/Users/azjezz/mago/mago/crates/type-syntax)
    Checking mago-reporting v1.0.0-rc.2 (/Users/azjezz/mago/mago/crates/reporting)
    Checking mago-syntax-core v1.0.0-rc.2 (/Users/azjezz/mago/mago/crates/syntax-core)
    Checking mago-span v1.0.0-rc.2 (/Users/azjezz/mago/mago/crates/span)
    Checking mago-database v1.0.0-rc.2 (/Users/azjezz/mago/mago/crates/database)
    Checking mago-php-version v1.0.0-rc.2 (/Users/azjezz/mago/mago/crates/php-version)
    Checking mago-composer v1.0.0-rc.2 (/Users/azjezz/mago/mago/crates/composer)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 54.20s

Version

rustc +nightly -vV
rustc 1.93.0-nightly (25d319a0f 2025-11-11)
binary: rustc
commit-hash: 25d319a0f656ee8faa7a534da299e76e96068a40
commit-date: 2025-11-11
host: aarch64-apple-darwin
release: 1.93.0-nightly
LLVM version: 21.1.5

Additional Labels

@rustbot label +C-bug +I-false-positive +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