Skip to content

std_instead_of_core: false positive with std::env since nightly-2024-03-08 #12438

@taiki-e

Description

@taiki-e

Summary

Since nightly-2024-03-08, it seems that std_instead_of_core is not correctly handling cases where macros and modules have the same name.

Probably related to #12406, cc @MarcusGrass

Lint Name

std_instead_of_core

Reproducer

I tried this code:

#![allow(dead_code)]
#![warn(clippy::std_instead_of_core)]

use std::env;

fn f() -> Option<String> {
    env::var("FOO").ok()
}

I saw this happen:

warning: used import from `std` instead of `core`
 --> src/lib.rs:4:5
  |
4 | use std::env;
  |     ^^^ help: consider importing the item from `core`: `core`
  |

However, replacing std::env with core::env causes compile error:

#![allow(dead_code)]
#![warn(clippy::std_instead_of_core)]

use core::env;

fn f() -> Option<String> {
    env::var("FOO").ok()
}
error[E0433]: failed to resolve: use of undeclared crate or module `env`
 --> src/lib.rs:7:5
  |
7 |     env::var("FOO").ok()
  |     ^^^ use of undeclared crate or module `env`
  |
help: consider importing this module
  |
4 + use std::env;
  |

I expected to see this happen: no warning

Version

rustc 1.78.0-nightly (9c3ad802d 2024-03-07)
binary: rustc
commit-hash: 9c3ad802d9b9633d60d3a74668eb1be819212d34
commit-date: 2024-03-07
host: aarch64-apple-darwin
release: 1.78.0-nightly
LLVM version: 18.1.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