|
| 1 | +// This file is part of Substrate. |
| 2 | + |
| 3 | +// Copyright (C) Parity Technologies (UK) Ltd. |
| 4 | +// SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | + |
| 18 | +//! Generates warnings for undesirable pallet code. |
| 19 | +
|
| 20 | +use crate::pallet::parse::call::{CallVariantDef, CallWeightDef}; |
| 21 | +use proc_macro_warning::Warning; |
| 22 | +use syn::{ |
| 23 | + spanned::Spanned, |
| 24 | + visit::{self, Visit}, |
| 25 | +}; |
| 26 | + |
| 27 | +/// Warn if any of the call arguments starts with a underscore and is used in a weight formula. |
| 28 | +pub(crate) fn weight_witness_warning( |
| 29 | + method: &CallVariantDef, |
| 30 | + dev_mode: bool, |
| 31 | + warnings: &mut Vec<Warning>, |
| 32 | +) { |
| 33 | + if dev_mode { |
| 34 | + return |
| 35 | + } |
| 36 | + let CallWeightDef::Immediate(w) = &method.weight else { |
| 37 | + return; |
| 38 | + }; |
| 39 | + |
| 40 | + let partial_warning = Warning::new_deprecated("UncheckedWeightWitness") |
| 41 | + .old("not check weight witness data") |
| 42 | + .new("ensure that all witness data for weight calculation is checked before usage") |
| 43 | + .help_link("https://github.com/paritytech/polkadot-sdk/pull/1818"); |
| 44 | + |
| 45 | + for (_, arg_ident, _) in method.args.iter() { |
| 46 | + if !arg_ident.to_string().starts_with('_') || !contains_ident(w.clone(), &arg_ident) { |
| 47 | + continue |
| 48 | + } |
| 49 | + |
| 50 | + let warning = partial_warning |
| 51 | + .clone() |
| 52 | + .index(warnings.len()) |
| 53 | + .span(arg_ident.span()) |
| 54 | + .build_or_panic(); |
| 55 | + |
| 56 | + warnings.push(warning); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/// Warn if the weight is a constant and the pallet not in `dev_mode`. |
| 61 | +pub(crate) fn weight_constant_warning( |
| 62 | + weight: &syn::Expr, |
| 63 | + dev_mode: bool, |
| 64 | + warnings: &mut Vec<Warning>, |
| 65 | +) { |
| 66 | + if dev_mode { |
| 67 | + return |
| 68 | + } |
| 69 | + let syn::Expr::Lit(lit) = weight else { |
| 70 | + return; |
| 71 | + }; |
| 72 | + |
| 73 | + let warning = Warning::new_deprecated("ConstantWeight") |
| 74 | + .index(warnings.len()) |
| 75 | + .old("use hard-coded constant as call weight") |
| 76 | + .new("benchmark all calls or put the pallet into `dev` mode") |
| 77 | + .help_link("https://github.com/paritytech/substrate/pull/13798") |
| 78 | + .span(lit.span()) |
| 79 | + .build_or_panic(); |
| 80 | + |
| 81 | + warnings.push(warning); |
| 82 | +} |
| 83 | + |
| 84 | +/// Returns whether `expr` contains `ident`. |
| 85 | +fn contains_ident(mut expr: syn::Expr, ident: &syn::Ident) -> bool { |
| 86 | + struct ContainsIdent { |
| 87 | + ident: syn::Ident, |
| 88 | + found: bool, |
| 89 | + } |
| 90 | + |
| 91 | + impl<'a> Visit<'a> for ContainsIdent { |
| 92 | + fn visit_ident(&mut self, i: &syn::Ident) { |
| 93 | + if *i == self.ident { |
| 94 | + self.found = true; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + let mut visitor = ContainsIdent { ident: ident.clone(), found: false }; |
| 100 | + visit::visit_expr(&mut visitor, &mut expr); |
| 101 | + visitor.found |
| 102 | +} |
0 commit comments