Skip to content

Commit cff5cff

Browse files
committed
Make the name of the crate available in cargo UI tests
1 parent c41916d commit cff5cff

File tree

7 files changed

+18
-5
lines changed

7 files changed

+18
-5
lines changed

clippy_dev/src/new_lint.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ fn create_test(lint: &LintData) -> io::Result<()> {
7676

7777
path.push("src");
7878
fs::create_dir(&path)?;
79-
write_file(path.join("main.rs"), get_test_file_contents(lint_name))?;
79+
let header = format!("// compile-flags: --crate-name={}", lint_name);
80+
write_file(path.join("main.rs"), get_test_file_contents(lint_name, Some(&header)))?;
8081

8182
Ok(())
8283
}
@@ -90,7 +91,7 @@ fn create_test(lint: &LintData) -> io::Result<()> {
9091
create_project_layout(lint.name, &test_dir, "pass", "This file should not trigger the lint")
9192
} else {
9293
let test_path = format!("tests/ui/{}.rs", lint.name);
93-
let test_contents = get_test_file_contents(lint.name);
94+
let test_contents = get_test_file_contents(lint.name, None);
9495
write_file(lint.project_root.join(test_path), test_contents)
9596
}
9697
}
@@ -119,16 +120,22 @@ fn to_camel_case(name: &str) -> String {
119120
.collect()
120121
}
121122

122-
fn get_test_file_contents(lint_name: &str) -> String {
123-
format!(
123+
fn get_test_file_contents(lint_name: &str, header_commands: Option<&str>) -> String {
124+
let mut contents = format!(
124125
"#![warn(clippy::{})]
125126
126127
fn main() {{
127128
// test code goes here
128129
}}
129130
",
130131
lint_name
131-
)
132+
);
133+
134+
if let Some(header) = header_commands {
135+
contents = format!("{}\n{}", header, contents);
136+
}
137+
138+
contents
132139
}
133140

134141
fn get_manifest_contents(lint_name: &str, hint: &str) -> String {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// compile-flags: --crate-name=cargo_common_metadata
12
#![warn(clippy::cargo_common_metadata)]
23

34
fn main() {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// compile-flags: --crate-name=cargo_common_metadata
12
#![warn(clippy::cargo_common_metadata)]
23

34
fn main() {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// compile-flags: --crate-name=multiple_crate_versions
12
#![warn(clippy::multiple_crate_versions)]
23

34
fn main() {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// compile-flags: --crate-name=multiple_crate_versions
12
#![warn(clippy::multiple_crate_versions)]
23

34
fn main() {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// compile-flags: --crate-name=wildcard_dependencies
12
#![warn(clippy::wildcard_dependencies)]
23

34
fn main() {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// compile-flags: --crate-name=wildcard_dependencies
12
#![warn(clippy::wildcard_dependencies)]
23

34
fn main() {}

0 commit comments

Comments
 (0)