Fix #[derive(Default)]
on a generic #[default]
enum adding unnecessary Default
bounds
This commit is contained in:
parent
5b4bd154de
commit
cb86c38cdb
12 changed files with 35 additions and 1 deletions
|
@ -16,6 +16,7 @@ pub fn expand_deriving_copy(
|
||||||
let trait_def = TraitDef {
|
let trait_def = TraitDef {
|
||||||
span,
|
span,
|
||||||
path: path_std!(marker::Copy),
|
path: path_std!(marker::Copy),
|
||||||
|
skip_path_as_bound: false,
|
||||||
additional_bounds: Vec::new(),
|
additional_bounds: Vec::new(),
|
||||||
generics: Bounds::empty(),
|
generics: Bounds::empty(),
|
||||||
supports_unions: true,
|
supports_unions: true,
|
||||||
|
|
|
@ -72,6 +72,7 @@ pub fn expand_deriving_clone(
|
||||||
let trait_def = TraitDef {
|
let trait_def = TraitDef {
|
||||||
span,
|
span,
|
||||||
path: path_std!(clone::Clone),
|
path: path_std!(clone::Clone),
|
||||||
|
skip_path_as_bound: false,
|
||||||
additional_bounds: bounds,
|
additional_bounds: bounds,
|
||||||
generics: Bounds::empty(),
|
generics: Bounds::empty(),
|
||||||
supports_unions: true,
|
supports_unions: true,
|
||||||
|
|
|
@ -25,6 +25,7 @@ pub fn expand_deriving_eq(
|
||||||
let trait_def = TraitDef {
|
let trait_def = TraitDef {
|
||||||
span,
|
span,
|
||||||
path: path_std!(cmp::Eq),
|
path: path_std!(cmp::Eq),
|
||||||
|
skip_path_as_bound: false,
|
||||||
additional_bounds: Vec::new(),
|
additional_bounds: Vec::new(),
|
||||||
generics: Bounds::empty(),
|
generics: Bounds::empty(),
|
||||||
supports_unions: true,
|
supports_unions: true,
|
||||||
|
|
|
@ -19,6 +19,7 @@ pub fn expand_deriving_ord(
|
||||||
let trait_def = TraitDef {
|
let trait_def = TraitDef {
|
||||||
span,
|
span,
|
||||||
path: path_std!(cmp::Ord),
|
path: path_std!(cmp::Ord),
|
||||||
|
skip_path_as_bound: false,
|
||||||
additional_bounds: Vec::new(),
|
additional_bounds: Vec::new(),
|
||||||
generics: Bounds::empty(),
|
generics: Bounds::empty(),
|
||||||
supports_unions: false,
|
supports_unions: false,
|
||||||
|
|
|
@ -83,6 +83,7 @@ pub fn expand_deriving_partial_eq(
|
||||||
let trait_def = TraitDef {
|
let trait_def = TraitDef {
|
||||||
span,
|
span,
|
||||||
path: path_std!(cmp::PartialEq),
|
path: path_std!(cmp::PartialEq),
|
||||||
|
skip_path_as_bound: false,
|
||||||
additional_bounds: Vec::new(),
|
additional_bounds: Vec::new(),
|
||||||
generics: Bounds::empty(),
|
generics: Bounds::empty(),
|
||||||
supports_unions: false,
|
supports_unions: false,
|
||||||
|
|
|
@ -37,6 +37,7 @@ pub fn expand_deriving_partial_ord(
|
||||||
let trait_def = TraitDef {
|
let trait_def = TraitDef {
|
||||||
span,
|
span,
|
||||||
path: path_std!(cmp::PartialOrd),
|
path: path_std!(cmp::PartialOrd),
|
||||||
|
skip_path_as_bound: false,
|
||||||
additional_bounds: vec![],
|
additional_bounds: vec![],
|
||||||
generics: Bounds::empty(),
|
generics: Bounds::empty(),
|
||||||
supports_unions: false,
|
supports_unions: false,
|
||||||
|
|
|
@ -20,6 +20,7 @@ pub fn expand_deriving_debug(
|
||||||
let trait_def = TraitDef {
|
let trait_def = TraitDef {
|
||||||
span,
|
span,
|
||||||
path: path_std!(fmt::Debug),
|
path: path_std!(fmt::Debug),
|
||||||
|
skip_path_as_bound: false,
|
||||||
additional_bounds: Vec::new(),
|
additional_bounds: Vec::new(),
|
||||||
generics: Bounds::empty(),
|
generics: Bounds::empty(),
|
||||||
supports_unions: false,
|
supports_unions: false,
|
||||||
|
|
|
@ -23,6 +23,7 @@ pub fn expand_deriving_rustc_decodable(
|
||||||
let trait_def = TraitDef {
|
let trait_def = TraitDef {
|
||||||
span,
|
span,
|
||||||
path: Path::new_(vec![krate, sym::Decodable], vec![], PathKind::Global),
|
path: Path::new_(vec![krate, sym::Decodable], vec![], PathKind::Global),
|
||||||
|
skip_path_as_bound: false,
|
||||||
additional_bounds: Vec::new(),
|
additional_bounds: Vec::new(),
|
||||||
generics: Bounds::empty(),
|
generics: Bounds::empty(),
|
||||||
supports_unions: false,
|
supports_unions: false,
|
||||||
|
|
|
@ -24,6 +24,7 @@ pub fn expand_deriving_default(
|
||||||
let trait_def = TraitDef {
|
let trait_def = TraitDef {
|
||||||
span,
|
span,
|
||||||
path: Path::new(vec![kw::Default, sym::Default]),
|
path: Path::new(vec![kw::Default, sym::Default]),
|
||||||
|
skip_path_as_bound: has_a_default_variant(item),
|
||||||
additional_bounds: Vec::new(),
|
additional_bounds: Vec::new(),
|
||||||
generics: Bounds::empty(),
|
generics: Bounds::empty(),
|
||||||
supports_unions: false,
|
supports_unions: false,
|
||||||
|
@ -262,3 +263,22 @@ impl<'a, 'b> rustc_ast::visit::Visitor<'a> for DetectNonVariantDefaultAttr<'a, '
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn has_a_default_variant(item: &Annotatable) -> bool {
|
||||||
|
struct HasDefaultAttrOnVariant {
|
||||||
|
found: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ast> rustc_ast::visit::Visitor<'ast> for HasDefaultAttrOnVariant {
|
||||||
|
fn visit_variant(&mut self, v: &'ast rustc_ast::Variant) {
|
||||||
|
if v.attrs.iter().any(|attr| attr.has_name(kw::Default)) {
|
||||||
|
self.found = true;
|
||||||
|
}
|
||||||
|
// no need to subrecurse.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut visitor = HasDefaultAttrOnVariant { found: false };
|
||||||
|
item.visit_with(&mut visitor);
|
||||||
|
visitor.found
|
||||||
|
}
|
||||||
|
|
|
@ -107,6 +107,7 @@ pub fn expand_deriving_rustc_encodable(
|
||||||
let trait_def = TraitDef {
|
let trait_def = TraitDef {
|
||||||
span,
|
span,
|
||||||
path: Path::new_(vec![krate, sym::Encodable], vec![], PathKind::Global),
|
path: Path::new_(vec![krate, sym::Encodable], vec![], PathKind::Global),
|
||||||
|
skip_path_as_bound: false,
|
||||||
additional_bounds: Vec::new(),
|
additional_bounds: Vec::new(),
|
||||||
generics: Bounds::empty(),
|
generics: Bounds::empty(),
|
||||||
supports_unions: false,
|
supports_unions: false,
|
||||||
|
|
|
@ -172,6 +172,7 @@ use rustc_span::symbol::{kw, sym, Ident, Symbol};
|
||||||
use rustc_span::Span;
|
use rustc_span::Span;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::iter;
|
use std::iter;
|
||||||
|
use std::ops::Not;
|
||||||
use std::vec;
|
use std::vec;
|
||||||
use thin_vec::thin_vec;
|
use thin_vec::thin_vec;
|
||||||
use ty::{Bounds, Path, Ref, Self_, Ty};
|
use ty::{Bounds, Path, Ref, Self_, Ty};
|
||||||
|
@ -185,6 +186,9 @@ pub struct TraitDef<'a> {
|
||||||
/// Path of the trait, including any type parameters
|
/// Path of the trait, including any type parameters
|
||||||
pub path: Path,
|
pub path: Path,
|
||||||
|
|
||||||
|
/// Whether to skip adding the current trait as a bound to the type parameters of the type.
|
||||||
|
pub skip_path_as_bound: bool,
|
||||||
|
|
||||||
/// Additional bounds required of any type parameters of the type,
|
/// Additional bounds required of any type parameters of the type,
|
||||||
/// other than the current trait
|
/// other than the current trait
|
||||||
pub additional_bounds: Vec<Ty>,
|
pub additional_bounds: Vec<Ty>,
|
||||||
|
@ -594,7 +598,7 @@ impl<'a> TraitDef<'a> {
|
||||||
cx.trait_bound(p.to_path(cx, self.span, type_ident, generics))
|
cx.trait_bound(p.to_path(cx, self.span, type_ident, generics))
|
||||||
}).chain(
|
}).chain(
|
||||||
// require the current trait
|
// require the current trait
|
||||||
iter::once(cx.trait_bound(trait_path.clone()))
|
self.skip_path_as_bound.not().then(|| cx.trait_bound(trait_path.clone()))
|
||||||
).chain(
|
).chain(
|
||||||
// also add in any bounds from the declaration
|
// also add in any bounds from the declaration
|
||||||
param.bounds.iter().cloned()
|
param.bounds.iter().cloned()
|
||||||
|
|
|
@ -22,6 +22,7 @@ pub fn expand_deriving_hash(
|
||||||
let hash_trait_def = TraitDef {
|
let hash_trait_def = TraitDef {
|
||||||
span,
|
span,
|
||||||
path,
|
path,
|
||||||
|
skip_path_as_bound: false,
|
||||||
additional_bounds: Vec::new(),
|
additional_bounds: Vec::new(),
|
||||||
generics: Bounds::empty(),
|
generics: Bounds::empty(),
|
||||||
supports_unions: false,
|
supports_unions: false,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue