Idea: Mark code as "known dead" to silence dead-code lint


#1

There should be a way to tag a function as “believed to be dead code”. This would silence the dead-code lint unless we actually found a use of the fn. This would be so useful when prototyping, when you have things that you know to be dead (because you didn’t write the consumer yet, or you commented it out). #[allow(dead_code)] isn’t quite right, because it tends to just hang out long after you’ve added the consumer (since you forget it’s there).


#2

There’s still the danger of retaining a forgotten allow in the case where the code actually is still dead, but I guess this is ultimately handled by forbid. Something like #[expect(dead_code)] would still provide more immediate feedback to remove the annotation when it’s no longer needed, which makes for less work once you eventually add forbid. I suppose the question is whether this is only applicable to the dead_code lint, or if other lints would benefit from a general syntax like expect. I think any time you silence a lint locally during development, being reminded to remove the annotation when it’s no longer needed could be useful.


#3

How about #[not_used]?

If found to be used the compiler should complain loudly.

(cf. #[used], rfc 2386)


No wait, this becomes confusing:

#[not_used]
#[must_use]
fn foo() -> i32 { ... }

We already have #[must_use] so, probably an attribute with a completely different name.


#4

Right idea – but yeah wrong name =)

I was thinking #[expect(dead_code)] or something.

Oooh, we could generalize this! Something like #[expect(lint)], which means that we expect a lint to fire on the annotated thing – and if it doesn’t, that is a warning.


#5

Which is exactly the second half of this RFC :laughing:


#6

I confess I hadn’t even seen that =)

I’ve been kind of caught up lately.


#7

Um… yeah! :sweat_smile:

Weird, I hadn’t seen that either. Must be a brainwave going around :smile:


#8

Some testing frameworks have an analogous concept; you mark a test as “pending”, and then when it fails, it tells the runner it’s passed. When it succeeds, however, it fails, alerting you that you thought this wasn’t done yet, but it does actually work.


#9

The expect() idea sounds nice.

But for the specific problem of compiler being annoying about unfinished code, I use this:

#![cfg_attr(debug_assertions, allow(dead_code, bad_style, unused)]

The debug assertions hack is supposed to enable this only while I’m working on it, but still warn me about issues before I release it. I wish there was some smarter logic for unfinished code/not-yet-useful lints.