rust-lang / rust-lang/rust-clippy
Suggest use of track_caller on assert-like functions
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
Suggest taking advantage of the #[track_caller] feature.
It's a less-known feature of Rust, but quite useful on assert-like functions which want to point out that their caller supplied invalid input. The attribute makes panics point to function's caller, e.g. a unit test, rather than internals of the helper function.
The attribute could be suggested on functions which contain panicking code (panic!, assert!) and are named assert_*. I'm not sure about the function name heuristic. Maybe it could be suggested on any "simple" panicking function, or functions called from any #[test], etc.
Categories (optional)
- Kind:
pedantic, maybestyle
Drawbacks
It's difficult to know for certain which functions are appropriate for use with #[track_caller].
Example
fn assert_not_zero(n: u32) {
assert!(n != 0);
}
Could be written as:
#[track_caller]
fn assert_not_zero(n: u32) {
assert!(n != 0);
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the proposed #[track_caller] use on assert_* functions and the alternatives involving panic!, assert!, or functions called from #[test]. Define a defensible scope and heuristic, then verify that the resulting lint correctly identifies suitable functions without flagging inappropriate cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100