Lint emitted in UI test but not when running against a separate package
mattvmsft opened this issue · comments
I am having some trouble getting a lint emitted when running cargo dylint --all
against a package. I have a small test lint as follows:
dylint_linting::declare_early_lint! {
pub LINT_EXAMPLE,
Warn,
"API banned_api is not allowed"
}
impl EarlyLintPass for LintExample {
fn check_fn(&mut self, cx: &rustc_lint::EarlyContext<'_>, fn_kind: rustc_ast::visit::FnKind<'_>, span: rustc_span::Span,_:rustc_ast::NodeId) {
if_chain! {
if let FnKind::Fn(_, ident, ..) = fn_kind;
if ident.name.as_str() == "banned_api";
then {
span_lint_and_sugg(cx, LINT_EXAMPLE, span, "This API is banned", "it can be replaced with", "allowed_api()".into(), Applicability::MachineApplicable)
}
}
}
}
When running the UI test for this lint on this main.rs file in my ui/
directory:
fn banned_api() {
}
fn main() {
banned_api()
}
the generated stderr file contains the lint I expect:
--> $DIR/main.rs:1:1
|
LL | / fn banned_api() {
LL | |
LL | | }
| |_^ help: it can be replaced with: `allowed_api()`
|
= note: `-D lint-example` implied by `-D warnings`
error: aborting due to previous error
(although as a side issue, I am unable to get the UI test to pass despite copying the generated .stderr into the main.stderr in my ui/
directory. The output does not have any + or - to denote differences between the .stderr files. Not sure if that is related.)
In a separate directory, I created a new Rust binary project where the main.rs file has the same contents as that of my ui test main.rs file, but when I run cargo dylint --all
I am simply getting:
Checking with toolchain `nightly-2022-09-29-x86_64-pc-windows-msvc`
Checking example_project v0.1.0 (<sample_proj_path>)
Finished dev [unoptimized + debuginfo] target(s) in 0.20s
without any additional output.
cargo dylint list
appears to find the correct lint library:
~#@❯ cargo dylint list
lint_example nightly-2022-09-29-x86_64-pc-windows-msvc <path_to_lint_dir>
Operating System: Windows 11
(although as a side issue, I am unable to get the UI test to pass despite copying the generated .stderr into the main.stderr in my ui/ directory. The output does not have any + or - to denote differences between the .stderr files. Not sure if that is related.)
Could you try changing the types of newlines that the .stderr
file uses (i.e., LF
vs. CRLF
) and see if that makes a difference?
In a separate directory, I created a new Rust binary project where the main.rs file has the same contents as that of my ui test main.rs file, but when I run cargo dylint --all I am simply getting...
By chance, have you tried cargo clean
and then cargo dylint --all
?
Cargo caches linting results, and I am wondering if that could be the problem.
Also, aside: are you tied to using an early lint?
I generally recommend writing late lints, unless you're absolutely sure your lint will never need typing and/or name resolution information.
Both of your suggestions resolved my issues, thank you!
Also, aside: are you tied to using an early lint?
I generally recommend writing late lints, unless you're absolutely sure your lint will never need typing and/or name resolution information.
In this case this was just for a small toy example to verify I could get everything running and partially following some documentation on lint passes from the Clippy book since I was just checking for function names in this example. I will keep your advice in mind when writing any non-example lints.
Thanks again for your quick response and taking the time to respond!