Make module-style lints resilient to --remap-path-prefix

This commit is contained in:
Wim Looman 2022-09-13 21:27:01 +02:00
parent 556415870d
commit e9722feef3
No known key found for this signature in database
GPG Key ID: C6F5748C6DD1607B
6 changed files with 32 additions and 3 deletions

View File

@ -2,7 +2,7 @@
use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_lint::{EarlyContext, EarlyLintPass, Level, LintContext}; use rustc_lint::{EarlyContext, EarlyLintPass, Level, LintContext};
use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::{FileName, RealFileName, SourceFile, Span, SyntaxContext}; use rustc_span::{FileName, SourceFile, Span, SyntaxContext};
use std::ffi::OsStr; use std::ffi::OsStr;
use std::path::{Component, Path}; use std::path::{Component, Path};
@ -79,7 +79,7 @@ fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &ast::Crate) {
let files = cx.sess().source_map().files(); let files = cx.sess().source_map().files();
let RealFileName::LocalPath(trim_to_src) = &cx.sess().opts.working_dir else { return }; let Some(trim_to_src) = cx.sess().opts.working_dir.local_path() else { return };
// `folder_segments` is all unique folder path segments `path/to/foo.rs` gives // `folder_segments` is all unique folder path segments `path/to/foo.rs` gives
// `[path, to]` but not foo // `[path, to]` but not foo
@ -90,7 +90,7 @@ fn check_crate(&mut self, cx: &EarlyContext<'_>, _: &ast::Crate) {
// `{ foo => path/to/foo.rs, .. } // `{ foo => path/to/foo.rs, .. }
let mut file_map = FxHashMap::default(); let mut file_map = FxHashMap::default();
for file in files.iter() { for file in files.iter() {
if let FileName::Real(RealFileName::LocalPath(lp)) = &file.name { if let FileName::Real(name) = &file.name && let Some(lp) = name.local_path() {
let path = if lp.is_relative() { let path = if lp.is_relative() {
lp lp
} else if let Ok(relative) = lp.strip_prefix(trim_to_src) { } else if let Ok(relative) = lp.strip_prefix(trim_to_src) {

View File

@ -0,0 +1,9 @@
[package]
name = "fail-mod-remap"
version = "0.1.0"
edition = "2018"
publish = false
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -0,0 +1 @@
pub mod inner;

View File

@ -0,0 +1 @@

View File

@ -0,0 +1,7 @@
// compile-flags: --remap-path-prefix {{src-base}}=/remapped
#![warn(clippy::self_named_module_files)]
mod bad;
fn main() {}

View File

@ -0,0 +1,11 @@
error: `mod.rs` files are required, found `bad.rs`
--> /remapped/module_style/fail_mod_remap/src/bad.rs:1:1
|
LL | pub mod inner;
| ^
|
= note: `-D clippy::self-named-module-files` implied by `-D warnings`
= help: move `bad.rs` to `bad/mod.rs`
error: aborting due to previous error