Skip to content

Commit ab0194f

Browse files
authored
[dropshot_endpoint] add allow(dead_code) to output trait
In general, we should treat the fact that our macro was invoked on the trait as enough of a use.
1 parent bde88ae commit ab0194f

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

dropshot_endpoint/src/api_trait.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,27 @@ impl<'ast> ApiParser<'ast> {
365365
let mut supertraits = item_trait.supertraits.clone();
366366
supertraits.push(parse_quote!('static));
367367

368+
// Also add dead_code if the visibility is not `pub`. (If it is `pub`,
369+
// then it is expected to be exported, and so the dead code warning
370+
// won't fire.)
371+
//
372+
// Why check for non-`pub` visibility? Because there is a downside to
373+
// allow(dead_code): it also applies to any items defined within the
374+
// trait. For example, if a provided method on the trait defines an
375+
// unused function inside of it, then allow(dead_code) would suppress
376+
// that.
377+
//
378+
// It would be ideal if there were a way to say "allow(dead_code), but
379+
// don't propagate this to child items", but sadly there isn't as of
380+
// Rust 1.81.
381+
let mut attrs = item_trait.attrs.clone();
382+
if !matches!(item_trait.vis, syn::Visibility::Public(_)) {
383+
attrs.push(parse_quote!(#[allow(dead_code)]));
384+
}
385+
368386
// Everything else about the trait stays the same -- just the items change.
369387
let out_trait = ItemTraitPartParsed {
388+
attrs,
370389
supertraits,
371390
items: out_items.collect(),
372391
..item_trait.clone()
@@ -1765,7 +1784,7 @@ mod tests {
17651784
let (item, errors) = do_trait(
17661785
quote! {},
17671786
quote! {
1768-
trait MyTrait {
1787+
pub trait MyTrait {
17691788
type Context;
17701789

17711790
#[endpoint {

dropshot_endpoint/tests/output/api_trait_basic.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#[allow(dead_code)]
12
trait MyTrait: 'static {
23
type Context: dropshot::ServerContext;
34
fn handler_xyz(

dropshot_endpoint/tests/output/api_trait_no_endpoints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#[allow(dead_code)]
12
pub(crate) trait MyTrait: 'static {
23
type Context: dropshot::ServerContext;
34
}

dropshot_endpoint/tests/output/api_trait_operation_id.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
trait MyTrait: 'static {
1+
pub trait MyTrait: 'static {
22
type Context: dropshot::ServerContext;
33
fn handler_xyz(
44
rqctx: RequestContext<Self::Context>,
@@ -12,7 +12,7 @@ trait MyTrait: 'static {
1212
}
1313
/// Support module for the Dropshot API trait [`MyTrait`](MyTrait).
1414
#[automatically_derived]
15-
mod my_trait_mod {
15+
pub mod my_trait_mod {
1616
use super::*;
1717
const _: fn() = || {
1818
trait TypeEq {

0 commit comments

Comments
 (0)