Run cargo fmt on the whole code base (#9394)

* Run cargo fmt on the whole code base

* Second run

* Add CI check

* Fix compilation

* More unnecessary braces

* Handle weights

* Use --all

* Use correct attributes...

* Fix UI tests

* AHHHHHHHHH

* 🤦

* Docs

* Fix compilation

* 🤷

* Please stop

* 🤦 x 2

* More

* make rustfmt.toml consistent with polkadot

Co-authored-by: André Silva <andrerfosilva@gmail.com>
This commit is contained in:
Bastian Köcher
2021-07-21 16:32:32 +02:00
committed by GitHub
parent d451c38c1c
commit 7b56ab15b4
1010 changed files with 53339 additions and 51208 deletions
@@ -15,11 +15,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use proc_macro2::{TokenStream, Span};
use proc_macro2::{Span, TokenStream};
use syn::{
Result, Ident, Signature, parse_quote, Type, Pat, spanned::Spanned, FnArg, Error, token::And,
ImplItem, ReturnType, PathArguments, Path, GenericArgument, TypePath, ItemImpl,
parse_quote, spanned::Spanned, token::And, Error, FnArg, GenericArgument, Ident, ImplItem,
ItemImpl, Pat, Path, PathArguments, Result, ReturnType, Signature, Type, TypePath,
};
use quote::quote;
@@ -49,18 +49,19 @@ pub fn generate_hidden_includes(unique_id: &'static str) -> TokenStream {
Err(e) => {
let err = Error::new(Span::call_site(), e).to_compile_error();
quote!( #err )
}
},
}
}
/// Generates the access to the `sc_client` crate.
pub fn generate_crate_access(unique_id: &'static str) -> TokenStream {
if env::var("CARGO_PKG_NAME").unwrap() == "sp-api" {
quote!( sp_api )
quote!(sp_api)
} else {
let mod_name = generate_hidden_includes_mod_name(unique_id);
quote!( self::#mod_name::sp_api )
}.into()
}
.into()
}
/// Generates the name of the module that contains the trait declaration for the runtime.
@@ -76,7 +77,7 @@ pub fn generate_method_runtime_api_impl_name(trait_: &Ident, method: &Ident) ->
/// Get the type of a `syn::ReturnType`.
pub fn return_type_extract_type(rt: &ReturnType) -> Type {
match rt {
ReturnType::Default => parse_quote!( () ),
ReturnType::Default => parse_quote!(()),
ReturnType::Type(_, ref ty) => *ty.clone(),
}
}
@@ -84,10 +85,13 @@ pub fn return_type_extract_type(rt: &ReturnType) -> Type {
/// Replace the `_` (wild card) parameter names in the given signature with unique identifiers.
pub fn replace_wild_card_parameter_names(input: &mut Signature) {
let mut generated_pattern_counter = 0;
input.inputs.iter_mut().for_each(|arg| if let FnArg::Typed(arg) = arg {
arg.pat = Box::new(
generate_unique_pattern((*arg.pat).clone(), &mut generated_pattern_counter),
);
input.inputs.iter_mut().for_each(|arg| {
if let FnArg::Typed(arg) = arg {
arg.pat = Box::new(generate_unique_pattern(
(*arg.pat).clone(),
&mut generated_pattern_counter,
));
}
});
}
@@ -101,7 +105,7 @@ pub fn fold_fn_decl_for_client_side(
// Add `&self, at:& BlockId` as parameters to each function at the beginning.
input.inputs.insert(0, parse_quote!( __runtime_api_at_param__: &#block_id ));
input.inputs.insert(0, parse_quote!( &self ));
input.inputs.insert(0, parse_quote!(&self));
// Wrap the output in a `Result`
input.output = {
@@ -114,10 +118,8 @@ pub fn fold_fn_decl_for_client_side(
pub fn generate_unique_pattern(pat: Pat, counter: &mut u32) -> Pat {
match pat {
Pat::Wild(_) => {
let generated_name = Ident::new(
&format!("__runtime_api_generated_name_{}__", counter),
pat.span(),
);
let generated_name =
Ident::new(&format!("__runtime_api_generated_name_{}__", counter), pat.span());
*counter += 1;
parse_quote!( #generated_name )
@@ -145,26 +147,20 @@ pub fn extract_parameter_names_types_and_borrows(
match input {
FnArg::Typed(arg) => {
let (ty, borrow) = match &*arg.ty {
Type::Reference(t) => {
((*t.elem).clone(), Some(t.and_token))
},
t => { (t.clone(), None) },
Type::Reference(t) => ((*t.elem).clone(), Some(t.and_token)),
t => (t.clone(), None),
};
let name = generate_unique_pattern(
(*arg.pat).clone(),
&mut generated_pattern_counter,
);
let name =
generate_unique_pattern((*arg.pat).clone(), &mut generated_pattern_counter);
result.push((name, ty, borrow));
},
FnArg::Receiver(_) if matches!(allow_self, AllowSelfRefInParameters::No) => {
return Err(Error::new(input.span(), "`self` parameter not supported!"))
},
FnArg::Receiver(recv) => {
FnArg::Receiver(_) if matches!(allow_self, AllowSelfRefInParameters::No) =>
return Err(Error::new(input.span(), "`self` parameter not supported!")),
FnArg::Receiver(recv) =>
if recv.mutability.is_some() || recv.reference.is_none() {
return Err(Error::new(recv.span(), "Only `&self` is supported!"))
}
},
},
}
}
@@ -190,7 +186,8 @@ pub fn prefix_function_with_trait<F: ToString>(trait_: &Ident, function: &F) ->
///
/// If a type is a reference, the inner type is extracted (without the reference).
pub fn extract_all_signature_types(items: &[ImplItem]) -> Vec<Type> {
items.iter()
items
.iter()
.filter_map(|i| match i {
ImplItem::Method(method) => Some(&method.sig),
_ => None,
@@ -201,13 +198,17 @@ pub fn extract_all_signature_types(items: &[ImplItem]) -> Vec<Type> {
ReturnType::Type(_, ty) => Some((**ty).clone()),
};
sig.inputs.iter().filter_map(|i| match i {
FnArg::Typed(arg) => Some(&arg.ty),
_ => None,
}).map(|ty| match &**ty {
Type::Reference(t) => (*t.elem).clone(),
_ => (**ty).clone(),
}).chain(ret_ty)
sig.inputs
.iter()
.filter_map(|i| match i {
FnArg::Typed(arg) => Some(&arg.ty),
_ => None,
})
.map(|ty| match &**ty {
Type::Reference(t) => (*t.elem).clone(),
_ => (**ty).clone(),
})
.chain(ret_ty)
})
.collect()
}
@@ -223,19 +224,20 @@ pub fn extract_block_type_from_trait_path(trait_: &Path) -> Result<&TypePath> {
.ok_or_else(|| Error::new(span, "Empty path not supported"))?;
match &generics.arguments {
PathArguments::AngleBracketed(ref args) => {
args.args.first().and_then(|v| match v {
PathArguments::AngleBracketed(ref args) => args
.args
.first()
.and_then(|v| match v {
GenericArgument::Type(Type::Path(ref block)) => Some(block),
_ => None
}).ok_or_else(|| Error::new(args.span(), "Missing `Block` generic parameter."))
},
_ => None,
})
.ok_or_else(|| Error::new(args.span(), "Missing `Block` generic parameter.")),
PathArguments::None => {
let span = trait_.segments.last().as_ref().unwrap().span();
Err(Error::new(span, "Missing `Block` generic parameter."))
},
PathArguments::Parenthesized(_) => {
Err(Error::new(generics.arguments.span(), "Unexpected parentheses in path!"))
},
PathArguments::Parenthesized(_) =>
Err(Error::new(generics.arguments.span(), "Unexpected parentheses in path!")),
}
}
@@ -252,19 +254,20 @@ pub fn extract_impl_trait<'a>(
impl_: &'a ItemImpl,
require: RequireQualifiedTraitPath,
) -> Result<&'a Path> {
impl_.trait_.as_ref().map(|v| &v.1).ok_or_else(
|| Error::new(impl_.span(), "Only implementation of traits are supported!")
).and_then(|p| {
if p.segments.len() > 1 || matches!(require, RequireQualifiedTraitPath::No) {
Ok(p)
} else {
Err(
Error::new(
impl_
.trait_
.as_ref()
.map(|v| &v.1)
.ok_or_else(|| Error::new(impl_.span(), "Only implementation of traits are supported!"))
.and_then(|p| {
if p.segments.len() > 1 || matches!(require, RequireQualifiedTraitPath::No) {
Ok(p)
} else {
Err(Error::new(
p.span(),
"The implemented trait has to be referenced with a path, \
e.g. `impl client::Core for Runtime`."
)
)
}
})
e.g. `impl client::Core for Runtime`.",
))
}
})
}