Skip to content

Commit 2f39264

Browse files
committed
clippy_dev: Set the current directory to clippy's root path.
1 parent bfc6ad0 commit 2f39264

File tree

8 files changed

+264
-127
lines changed

8 files changed

+264
-127
lines changed

clippy_dev/src/dogfood.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{clippy_project_root, exit_if_err};
1+
use crate::utils::exit_if_err;
22
use std::process::Command;
33

44
/// # Panics
@@ -8,8 +8,7 @@ use std::process::Command;
88
pub fn dogfood(fix: bool, allow_dirty: bool, allow_staged: bool, allow_no_vcs: bool) {
99
let mut cmd = Command::new("cargo");
1010

11-
cmd.current_dir(clippy_project_root())
12-
.args(["test", "--test", "dogfood"])
11+
cmd.args(["test", "--test", "dogfood"])
1312
.args(["--features", "internal"])
1413
.args(["--", "dogfood_clippy", "--nocapture"]);
1514

clippy_dev/src/fmt.rs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::utils::clippy_project_root;
21
use itertools::Itertools;
32
use rustc_lexer::{TokenKind, tokenize};
43
use shell_escape::escape;
@@ -104,15 +103,8 @@ fn fmt_conf(check: bool) -> Result<(), Error> {
104103
Field,
105104
}
106105

107-
let path: PathBuf = [
108-
clippy_project_root().as_path(),
109-
"clippy_config".as_ref(),
110-
"src".as_ref(),
111-
"conf.rs".as_ref(),
112-
]
113-
.into_iter()
114-
.collect();
115-
let text = fs::read_to_string(&path)?;
106+
let path = "clippy_config/src/conf.rs";
107+
let text = fs::read_to_string(path)?;
116108

117109
let (pre, conf) = text
118110
.split_once("define_Conf! {\n")
@@ -203,7 +195,7 @@ fn fmt_conf(check: bool) -> Result<(), Error> {
203195
| (State::Lints, TokenKind::Comma | TokenKind::OpenParen | TokenKind::CloseParen) => {},
204196
_ => {
205197
return Err(Error::Parse(
206-
path,
198+
PathBuf::from(path),
207199
offset_to_line(&text, conf_offset + i),
208200
format!("unexpected token `{}`", &conf[i..i + t.len as usize]),
209201
));
@@ -213,7 +205,7 @@ fn fmt_conf(check: bool) -> Result<(), Error> {
213205

214206
if !matches!(state, State::Field) {
215207
return Err(Error::Parse(
216-
path,
208+
PathBuf::from(path),
217209
offset_to_line(&text, conf_offset + conf.len()),
218210
"incomplete field".into(),
219211
));
@@ -260,18 +252,16 @@ fn fmt_conf(check: bool) -> Result<(), Error> {
260252
if check {
261253
return Err(Error::CheckFailed);
262254
}
263-
fs::write(&path, new_text.as_bytes())?;
255+
fs::write(path, new_text.as_bytes())?;
264256
}
265257
Ok(())
266258
}
267259

268260
fn run_rustfmt(context: &FmtContext) -> Result<(), Error> {
269-
let project_root = clippy_project_root();
270-
271261
// if we added a local rustc repo as path dependency to clippy for rust analyzer, we do NOT want to
272262
// format because rustfmt would also format the entire rustc repo as it is a local
273263
// dependency
274-
if fs::read_to_string(project_root.join("Cargo.toml"))
264+
if fs::read_to_string("Cargo.toml")
275265
.expect("Failed to read clippy Cargo.toml")
276266
.contains("[target.'cfg(NOT_A_PLATFORM)'.dependencies]")
277267
{
@@ -280,12 +270,12 @@ fn run_rustfmt(context: &FmtContext) -> Result<(), Error> {
280270

281271
check_for_rustfmt(context)?;
282272

283-
cargo_fmt(context, project_root.as_path())?;
284-
cargo_fmt(context, &project_root.join("clippy_dev"))?;
285-
cargo_fmt(context, &project_root.join("rustc_tools_util"))?;
286-
cargo_fmt(context, &project_root.join("lintcheck"))?;
273+
cargo_fmt(context, ".".as_ref())?;
274+
cargo_fmt(context, "clippy_dev".as_ref())?;
275+
cargo_fmt(context, "rustc_tools_util".as_ref())?;
276+
cargo_fmt(context, "lintcheck".as_ref())?;
287277

288-
let chunks = WalkDir::new(project_root.join("tests"))
278+
let chunks = WalkDir::new("tests")
289279
.into_iter()
290280
.filter_map(|entry| {
291281
let entry = entry.expect("failed to find tests");

clippy_dev/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(rustc_private)]
1+
#![feature(rustc_private, if_let_guard, let_chains)]
22
#![warn(
33
trivial_casts,
44
trivial_numeric_casts,

clippy_dev/src/main.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55
use clap::{Args, Parser, Subcommand};
66
use clippy_dev::{dogfood, fmt, lint, new_lint, release, serve, setup, sync, update_lints, utils};
77
use std::convert::Infallible;
8+
use std::env;
89

910
fn main() {
1011
let dev = Dev::parse();
12+
let clippy = utils::ClippyInfo::search_for_manifest();
13+
if let Err(e) = env::set_current_dir(&clippy.path) {
14+
panic!("error setting current directory to `{}`: {e}", clippy.path.display());
15+
}
1116

1217
match dev.command {
1318
DevCommand::Bless => {
@@ -35,7 +40,7 @@ fn main() {
3540
category,
3641
r#type,
3742
msrv,
38-
} => match new_lint::create(pass, &name, &category, r#type.as_deref(), msrv) {
43+
} => match new_lint::create(clippy.version, pass, &name, &category, r#type.as_deref(), msrv) {
3944
Ok(()) => update_lints::update(utils::UpdateMode::Change),
4045
Err(e) => eprintln!("Unable to create lint: {e}"),
4146
},
@@ -79,13 +84,18 @@ fn main() {
7984
old_name,
8085
new_name,
8186
uplift,
82-
} => update_lints::rename(&old_name, new_name.as_ref().unwrap_or(&old_name), uplift),
83-
DevCommand::Deprecate { name, reason } => update_lints::deprecate(&name, &reason),
87+
} => update_lints::rename(
88+
clippy.version,
89+
&old_name,
90+
new_name.as_ref().unwrap_or(&old_name),
91+
uplift,
92+
),
93+
DevCommand::Deprecate { name, reason } => update_lints::deprecate(clippy.version, &name, &reason),
8494
DevCommand::Sync(SyncCommand { subcommand }) => match subcommand {
8595
SyncSubcommand::UpdateNightly => sync::update_nightly(),
8696
},
8797
DevCommand::Release(ReleaseCommand { subcommand }) => match subcommand {
88-
ReleaseSubcommand::BumpVersion => release::bump_version(),
98+
ReleaseSubcommand::BumpVersion => release::bump_version(clippy.version),
8999
},
90100
}
91101
}

clippy_dev/src/new_lint.rs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{clippy_project_root, clippy_version};
1+
use crate::utils::Version;
22
use clap::ValueEnum;
33
use indoc::{formatdoc, writedoc};
44
use std::fmt::{self, Write as _};
@@ -22,11 +22,11 @@ impl fmt::Display for Pass {
2222
}
2323

2424
struct LintData<'a> {
25+
clippy_version: Version,
2526
pass: Pass,
2627
name: &'a str,
2728
category: &'a str,
2829
ty: Option<&'a str>,
29-
project_root: PathBuf,
3030
}
3131

3232
trait Context {
@@ -50,18 +50,25 @@ impl<T> Context for io::Result<T> {
5050
/// # Errors
5151
///
5252
/// This function errors out if the files couldn't be created or written to.
53-
pub fn create(pass: Pass, name: &str, category: &str, mut ty: Option<&str>, msrv: bool) -> io::Result<()> {
53+
pub fn create(
54+
clippy_version: Version,
55+
pass: Pass,
56+
name: &str,
57+
category: &str,
58+
mut ty: Option<&str>,
59+
msrv: bool,
60+
) -> io::Result<()> {
5461
if category == "cargo" && ty.is_none() {
5562
// `cargo` is a special category, these lints should always be in `clippy_lints/src/cargo`
5663
ty = Some("cargo");
5764
}
5865

5966
let lint = LintData {
67+
clippy_version,
6068
pass,
6169
name,
6270
category,
6371
ty,
64-
project_root: clippy_project_root(),
6572
};
6673

6774
create_lint(&lint, msrv).context("Unable to create lint implementation")?;
@@ -88,7 +95,7 @@ fn create_lint(lint: &LintData<'_>, enable_msrv: bool) -> io::Result<()> {
8895
} else {
8996
let lint_contents = get_lint_file_contents(lint, enable_msrv);
9097
let lint_path = format!("clippy_lints/src/{}.rs", lint.name);
91-
write_file(lint.project_root.join(&lint_path), lint_contents.as_bytes())?;
98+
write_file(&lint_path, lint_contents.as_bytes())?;
9299
println!("Generated lint file: `{lint_path}`");
93100

94101
Ok(())
@@ -115,8 +122,7 @@ fn create_test(lint: &LintData<'_>, msrv: bool) -> io::Result<()> {
115122
}
116123

117124
if lint.category == "cargo" {
118-
let relative_test_dir = format!("tests/ui-cargo/{}", lint.name);
119-
let test_dir = lint.project_root.join(&relative_test_dir);
125+
let test_dir = format!("tests/ui-cargo/{}", lint.name);
120126
fs::create_dir(&test_dir)?;
121127

122128
create_project_layout(
@@ -134,11 +140,11 @@ fn create_test(lint: &LintData<'_>, msrv: bool) -> io::Result<()> {
134140
false,
135141
)?;
136142

137-
println!("Generated test directories: `{relative_test_dir}/pass`, `{relative_test_dir}/fail`");
143+
println!("Generated test directories: `{test_dir}/pass`, `{test_dir}/fail`");
138144
} else {
139145
let test_path = format!("tests/ui/{}.rs", lint.name);
140146
let test_contents = get_test_file_contents(lint.name, msrv);
141-
write_file(lint.project_root.join(&test_path), test_contents)?;
147+
write_file(&test_path, test_contents)?;
142148

143149
println!("Generated test file: `{test_path}`");
144150
}
@@ -193,11 +199,6 @@ fn to_camel_case(name: &str) -> String {
193199
.collect()
194200
}
195201

196-
pub(crate) fn get_stabilization_version() -> String {
197-
let (minor, patch) = clippy_version();
198-
format!("{minor}.{patch}.0")
199-
}
200-
201202
fn get_test_file_contents(lint_name: &str, msrv: bool) -> String {
202203
let mut test = formatdoc!(
203204
r"
@@ -292,7 +293,11 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
292293
);
293294
}
294295

295-
let _: fmt::Result = writeln!(result, "{}", get_lint_declaration(&name_upper, category));
296+
let _: fmt::Result = writeln!(
297+
result,
298+
"{}",
299+
get_lint_declaration(lint.clippy_version, &name_upper, category)
300+
);
296301

297302
if enable_msrv {
298303
let _: fmt::Result = writedoc!(
@@ -330,7 +335,7 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
330335
result
331336
}
332337

333-
fn get_lint_declaration(name_upper: &str, category: &str) -> String {
338+
fn get_lint_declaration(version: Version, name_upper: &str, category: &str) -> String {
334339
let justification_heading = if category == "restriction" {
335340
"Why restrict this?"
336341
} else {
@@ -357,7 +362,7 @@ fn get_lint_declaration(name_upper: &str, category: &str) -> String {
357362
"default lint description"
358363
}}
359364
"#,
360-
get_stabilization_version(),
365+
version.rust_display(),
361366
)
362367
}
363368

@@ -371,7 +376,7 @@ fn create_lint_for_ty(lint: &LintData<'_>, enable_msrv: bool, ty: &str) -> io::R
371376
_ => {},
372377
}
373378

374-
let ty_dir = lint.project_root.join(format!("clippy_lints/src/{ty}"));
379+
let ty_dir = PathBuf::from(format!("clippy_lints/src/{ty}"));
375380
assert!(
376381
ty_dir.exists() && ty_dir.is_dir(),
377382
"Directory `{}` does not exist!",
@@ -529,7 +534,10 @@ fn setup_mod_file(path: &Path, lint: &LintData<'_>) -> io::Result<&'static str>
529534
file_contents.replace_range(
530535
// Remove the trailing newline, which should always be present
531536
last_decl_curly_offset..=last_decl_curly_offset,
532-
&format!("\n\n{}", get_lint_declaration(&lint_name_upper, lint.category)),
537+
&format!(
538+
"\n\n{}",
539+
get_lint_declaration(lint.clippy_version, &lint_name_upper, lint.category)
540+
),
533541
);
534542

535543
// Add the lint to `impl_lint_pass`/`declare_lint_pass`

clippy_dev/src/release.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::fmt::Write;
2-
use std::path::Path;
32

4-
use crate::utils::{UpdateMode, clippy_version, replace_region_in_file};
3+
use crate::utils::{UpdateMode, Version, replace_region_in_file};
54

65
const CARGO_TOML_FILES: [&str; 4] = [
76
"clippy_config/Cargo.toml",
@@ -10,17 +9,16 @@ const CARGO_TOML_FILES: [&str; 4] = [
109
"Cargo.toml",
1110
];
1211

13-
pub fn bump_version() {
14-
let (minor, mut patch) = clippy_version();
15-
patch += 1;
16-
for file in &CARGO_TOML_FILES {
12+
pub fn bump_version(mut version: Version) {
13+
version.minor += 1;
14+
for &file in &CARGO_TOML_FILES {
1715
replace_region_in_file(
1816
UpdateMode::Change,
19-
Path::new(file),
17+
file.as_ref(),
2018
"# begin autogenerated version\n",
2119
"# end autogenerated version",
2220
|res| {
23-
writeln!(res, "version = \"0.{minor}.{patch}\"").unwrap();
21+
writeln!(res, "version = \"{}\"", version.toml_display()).unwrap();
2422
},
2523
);
2624
}

0 commit comments

Comments
 (0)