missing_headers: skip linting test functions, functions in test modules, and functions with #[cfg(test)] to avoid false positives#16704
Conversation
Add logic to skip the missing_headers lint for test functions, functions inside test modules, or functions with #[cfg(test)] to avoid false positives on test code.
|
Some changes occurred in clippy_lints/src/doc cc @notriddle |
|
r? @llogiq rustbot has assigned @llogiq. Use Why was this reviewer chosen?The reviewer was selected based on:
|
missing_headers: skip linting test functions, functions in test modules, and functions with #[cfg(test)] to avoid false positives
|
I was looking at creating a fix for this too. // Do not lint if any parent has `#[test]` attribute (#12265)
if let Some(body_id) = body_id
&& is_in_test_function(cx.tcx, body_id.hir_id)
{
return;
}But your solution seems to cover some other cases like Also make sure to add tests to the case to prevent future regressions. Something like this will do: /// Should not lint on test functions even with check-private-items
#[test]
fn test_with_panic() {
let result: Result<i32, &str> = Ok(1);
result.unwrap();
} |
|
I asked my LLM to validate your solution 😅
Maybe that helps with reviewing. I think the first case might be an uncommon by valid case to consider. |
Use `is_in_test` instead of multiple separate functions (`is_test_function`, `is_cfg_test`, `is_in_cfg_test`) to check if a function should be skipped from linting. This also fixes the lint to properly skip functions with `#[test]` attribute or in `#[cfg(test)]` blocks per issue rust-lang#12265.
|
|
Thanks for your feedback, @ralpha! Your implementation looks kind of cleaner than mine, but would miss Shouldn't we also suppress this function? I've adapted your changes to avoid sending a warning for that function which also addresses all the bullet points off your LLM review btw. :) |
|
Nice, I think the current solution Thanks for fixing this. I see all the tests pass now too, even with the added test case. Hope to see the merged soon 🙌 |
|
Any chance of this being merged soon, @llogiq? Or is there anything that is missing 😄 ? |
Summary
This PR fixes unnecessary warnings for
missing_headerson test code.Changes
Add logic to skip the
missing_headerslint for:test_*or decorated with#[test])mod tests { ... })#[cfg(test)]attributeThis avoids triggering the lint on test code where documentation headers are often unnecessary.
The logic should affect these lints:
missing_errors_docmissing_panics_docmissing_safety_docunnecessary_safety_docfixes #13391 and #12265
changelog: [
missing_headers]: skip linting test functions, functions in test modules, and functions with#[cfg(test)]to avoid false positives