Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dropshot_endpoint] add allow(dead_code) to output trait #1137

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion dropshot_endpoint/src/api_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,27 @@ impl<'ast> ApiParser<'ast> {
let mut supertraits = item_trait.supertraits.clone();
supertraits.push(parse_quote!('static));

// Also add dead_code if the visibility is not `pub`. (If it is `pub`,
// then it is expected to be exported, and so the dead code warning
// won't fire.)
//
// Why check for non-`pub` visibility? Because there is a downside to
// allow(dead_code): it also applies to any items defined within the
// trait. For example, if a provided method on the trait defines an
// unused function inside of it, then allow(dead_code) would suppress
// that.
//
// It would be ideal if there were a way to say "allow(dead_code), but
// don't propagate this to child items", but sadly there isn't as of
// Rust 1.81.
let mut attrs = item_trait.attrs.clone();
if !matches!(item_trait.vis, syn::Visibility::Public(_)) {
attrs.push(parse_quote!(#[allow(dead_code)]));
}

// Everything else about the trait stays the same -- just the items change.
let out_trait = ItemTraitPartParsed {
attrs,
supertraits,
items: out_items.collect(),
..item_trait.clone()
Expand Down Expand Up @@ -1765,7 +1784,7 @@ mod tests {
let (item, errors) = do_trait(
quote! {},
quote! {
trait MyTrait {
pub trait MyTrait {
type Context;

#[endpoint {
Expand Down
1 change: 1 addition & 0 deletions dropshot_endpoint/tests/output/api_trait_basic.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[allow(dead_code)]
trait MyTrait: 'static {
type Context: dropshot::ServerContext;
fn handler_xyz(
Expand Down
1 change: 1 addition & 0 deletions dropshot_endpoint/tests/output/api_trait_no_endpoints.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[allow(dead_code)]
pub(crate) trait MyTrait: 'static {
type Context: dropshot::ServerContext;
}
Expand Down
4 changes: 2 additions & 2 deletions dropshot_endpoint/tests/output/api_trait_operation_id.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
trait MyTrait: 'static {
pub trait MyTrait: 'static {
type Context: dropshot::ServerContext;
fn handler_xyz(
rqctx: RequestContext<Self::Context>,
Expand All @@ -12,7 +12,7 @@ trait MyTrait: 'static {
}
/// Support module for the Dropshot API trait [`MyTrait`](MyTrait).
#[automatically_derived]
mod my_trait_mod {
pub mod my_trait_mod {
use super::*;
const _: fn() = || {
trait TypeEq {
Expand Down