2019-09-18 06:37:41 +00:00
|
|
|
use crate::utils::{
|
2020-10-23 20:16:59 +00:00
|
|
|
attr_by_name, attrs::is_proc_macro, is_must_use_ty, is_trait_impl_item, is_type_diagnostic_item, iter_input_pats,
|
|
|
|
last_path_segment, match_def_path, must_use_attr, qpath_res, return_ty, snippet, snippet_opt, span_lint,
|
|
|
|
span_lint_and_help, span_lint_and_then, trait_ref_of_method, type_is_unsafe_function,
|
2019-09-18 06:37:41 +00:00
|
|
|
};
|
2020-10-23 20:16:59 +00:00
|
|
|
use if_chain::if_chain;
|
2020-03-01 03:23:33 +00:00
|
|
|
use rustc_ast::ast::Attribute;
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2019-09-18 06:37:41 +00:00
|
|
|
use rustc_errors::Applicability;
|
2020-01-06 16:39:50 +00:00
|
|
|
use rustc_hir as hir;
|
2020-01-09 07:13:22 +00:00
|
|
|
use rustc_hir::intravisit;
|
2020-01-06 16:39:50 +00:00
|
|
|
use rustc_hir::{def::Res, def_id::DefId};
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
2020-03-30 09:02:14 +00:00
|
|
|
use rustc_middle::hir::map::Map;
|
|
|
|
use rustc_middle::lint::in_external_macro;
|
|
|
|
use rustc_middle::ty::{self, Ty};
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2020-01-04 10:00:00 +00:00
|
|
|
use rustc_span::source_map::Span;
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2020-10-23 20:16:59 +00:00
|
|
|
use rustc_typeck::hir_ty_to_ty;
|
2016-03-08 23:48:10 +00:00
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Checks for functions with too many parameters.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Functions with lots of parameters are considered bad
|
|
|
|
/// style and reduce readability (“what does the 5th parameter mean?”). Consider
|
|
|
|
/// grouping some parameters into a new type.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
2019-08-02 06:13:54 +00:00
|
|
|
/// # struct Color;
|
2019-03-05 16:50:33 +00:00
|
|
|
/// fn foo(x: u32, y: u32, name: &str, c: Color, w: f32, h: f32, a: f32, b: f32) {
|
2019-08-02 06:13:54 +00:00
|
|
|
/// // ..
|
2019-03-05 16:50:33 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
2016-03-08 23:48:10 +00:00
|
|
|
pub TOO_MANY_ARGUMENTS,
|
2018-03-29 11:41:53 +00:00
|
|
|
complexity,
|
2016-03-08 23:48:10 +00:00
|
|
|
"functions with too many arguments"
|
|
|
|
}
|
|
|
|
|
2019-01-13 15:19:02 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Checks for functions with a large amount of lines.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Functions with a lot of lines are harder to understand
|
|
|
|
/// due to having to look at a larger amount of code to understand what the
|
|
|
|
/// function is doing. Consider splitting the body of the function into
|
|
|
|
/// multiple functions.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
2020-06-09 14:36:01 +00:00
|
|
|
/// ```rust
|
2019-03-05 16:50:33 +00:00
|
|
|
/// fn im_too_long() {
|
2020-06-09 14:36:01 +00:00
|
|
|
/// println!("");
|
|
|
|
/// // ... 100 more LoC
|
|
|
|
/// println!("");
|
2019-03-05 16:50:33 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
2019-01-13 15:19:02 +00:00
|
|
|
pub TOO_MANY_LINES,
|
|
|
|
pedantic,
|
|
|
|
"functions with too many lines"
|
|
|
|
}
|
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-06-12 18:07:10 +00:00
|
|
|
/// **What it does:** Checks for public functions that dereference raw pointer
|
2019-03-05 16:50:33 +00:00
|
|
|
/// arguments but are not marked unsafe.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** The function should probably be marked `unsafe`, since
|
|
|
|
/// for an arbitrary raw pointer, there is no way of telling for sure if it is
|
|
|
|
/// valid.
|
|
|
|
///
|
|
|
|
/// **Known problems:**
|
|
|
|
///
|
|
|
|
/// * It does not check functions recursively so if the pointer is passed to a
|
|
|
|
/// private non-`unsafe` function which does the dereferencing, the lint won't
|
|
|
|
/// trigger.
|
|
|
|
/// * It only checks for arguments whose type are raw pointers, not raw pointers
|
|
|
|
/// got from an argument in some other way (`fn foo(bar: &[*const u8])` or
|
|
|
|
/// `some_argument.get_raw_ptr()`).
|
|
|
|
///
|
|
|
|
/// **Example:**
|
2020-06-09 14:36:01 +00:00
|
|
|
/// ```rust,ignore
|
|
|
|
/// // Bad
|
2019-03-05 16:50:33 +00:00
|
|
|
/// pub fn foo(x: *const u8) {
|
|
|
|
/// println!("{}", unsafe { *x });
|
|
|
|
/// }
|
2020-06-09 14:36:01 +00:00
|
|
|
///
|
|
|
|
/// // Good
|
|
|
|
/// pub unsafe fn foo(x: *const u8) {
|
|
|
|
/// println!("{}", unsafe { *x });
|
|
|
|
/// }
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```
|
2016-06-07 14:55:55 +00:00
|
|
|
pub NOT_UNSAFE_PTR_ARG_DEREF,
|
2018-03-28 13:24:26 +00:00
|
|
|
correctness,
|
2016-06-07 14:55:55 +00:00
|
|
|
"public functions dereferencing raw pointer arguments but not marked `unsafe`"
|
|
|
|
}
|
|
|
|
|
2019-09-18 06:37:41 +00:00
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Checks for a [`#[must_use]`] attribute on
|
|
|
|
/// unit-returning functions and methods.
|
|
|
|
///
|
|
|
|
/// [`#[must_use]`]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Unit values are useless. The attribute is likely
|
|
|
|
/// a remnant of a refactoring that removed the return type.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Examples:**
|
|
|
|
/// ```rust
|
|
|
|
/// #[must_use]
|
|
|
|
/// fn useless() { }
|
|
|
|
/// ```
|
|
|
|
pub MUST_USE_UNIT,
|
|
|
|
style,
|
|
|
|
"`#[must_use]` attribute on a unit-returning function / method"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Checks for a [`#[must_use]`] attribute without
|
|
|
|
/// further information on functions and methods that return a type already
|
|
|
|
/// marked as `#[must_use]`.
|
|
|
|
///
|
|
|
|
/// [`#[must_use]`]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** The attribute isn't needed. Not using the result
|
|
|
|
/// will already be reported. Alternatively, one can add some text to the
|
|
|
|
/// attribute to improve the lint message.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Examples:**
|
|
|
|
/// ```rust
|
|
|
|
/// #[must_use]
|
|
|
|
/// fn double_must_use() -> Result<(), ()> {
|
|
|
|
/// unimplemented!();
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub DOUBLE_MUST_USE,
|
|
|
|
style,
|
|
|
|
"`#[must_use]` attribute on a `#[must_use]`-returning function / method"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Checks for public functions that have no
|
|
|
|
/// [`#[must_use]`] attribute, but return something not already marked
|
|
|
|
/// must-use, have no mutable arg and mutate no statics.
|
|
|
|
///
|
|
|
|
/// [`#[must_use]`]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Not bad at all, this lint just shows places where
|
|
|
|
/// you could add the attribute.
|
|
|
|
///
|
|
|
|
/// **Known problems:** The lint only checks the arguments for mutable
|
|
|
|
/// types without looking if they are actually changed. On the other hand,
|
|
|
|
/// it also ignores a broad range of potentially interesting side effects,
|
|
|
|
/// because we cannot decide whether the programmer intends the function to
|
|
|
|
/// be called for the side effect or the result. Expect many false
|
|
|
|
/// positives. At least we don't lint if the result type is unit or already
|
|
|
|
/// `#[must_use]`.
|
|
|
|
///
|
|
|
|
/// **Examples:**
|
|
|
|
/// ```rust
|
|
|
|
/// // this could be annotated with `#[must_use]`.
|
|
|
|
/// fn id<T>(t: T) -> T { t }
|
|
|
|
/// ```
|
|
|
|
pub MUST_USE_CANDIDATE,
|
|
|
|
pedantic,
|
|
|
|
"function or method that could take a `#[must_use]` attribute"
|
|
|
|
}
|
|
|
|
|
2020-10-23 20:16:59 +00:00
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Checks for public functions that return a `Result`
|
|
|
|
/// with an `Err` type of `()`. It suggests using a custom type that
|
|
|
|
/// implements [`std::error::Error`].
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Unit does not implement `Error` and carries no
|
|
|
|
/// further information about what went wrong.
|
|
|
|
///
|
|
|
|
/// **Known problems:** Of course, this lint assumes that `Result` is used
|
|
|
|
/// for a fallible operation (which is after all the intended use). However
|
|
|
|
/// code may opt to (mis)use it as a basic two-variant-enum. In that case,
|
|
|
|
/// the suggestion is misguided, and the code should use a custom enum
|
|
|
|
/// instead.
|
|
|
|
///
|
|
|
|
/// **Examples:**
|
|
|
|
/// ```rust
|
|
|
|
/// pub fn read_u8() -> Result<u8, ()> { Err(()) }
|
|
|
|
/// ```
|
|
|
|
/// should become
|
|
|
|
/// ```rust,should_panic
|
|
|
|
/// use std::fmt;
|
|
|
|
///
|
|
|
|
/// #[derive(Debug)]
|
|
|
|
/// pub struct EndOfStream;
|
|
|
|
///
|
|
|
|
/// impl fmt::Display for EndOfStream {
|
|
|
|
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
/// write!(f, "End of Stream")
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl std::error::Error for EndOfStream { }
|
|
|
|
///
|
|
|
|
/// pub fn read_u8() -> Result<u8, EndOfStream> { Err(EndOfStream) }
|
|
|
|
///# fn main() {
|
|
|
|
///# read_u8().unwrap();
|
|
|
|
///# }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Note that there are crates that simplify creating the error type, e.g.
|
|
|
|
/// [`thiserror`](https://docs.rs/thiserror).
|
|
|
|
pub RESULT_UNIT_ERR,
|
|
|
|
style,
|
|
|
|
"public function returning `Result` with an `Err` type of `()`"
|
|
|
|
}
|
|
|
|
|
2017-08-09 07:30:56 +00:00
|
|
|
#[derive(Copy, Clone)]
|
2016-03-08 23:48:10 +00:00
|
|
|
pub struct Functions {
|
|
|
|
threshold: u64,
|
2019-01-13 21:53:26 +00:00
|
|
|
max_lines: u64,
|
2016-03-08 23:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Functions {
|
2019-01-13 15:19:02 +00:00
|
|
|
pub fn new(threshold: u64, max_lines: u64) -> Self {
|
2019-01-13 21:53:26 +00:00
|
|
|
Self { threshold, max_lines }
|
2016-03-08 23:48:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 06:37:41 +00:00
|
|
|
impl_lint_pass!(Functions => [
|
|
|
|
TOO_MANY_ARGUMENTS,
|
|
|
|
TOO_MANY_LINES,
|
|
|
|
NOT_UNSAFE_PTR_ARG_DEREF,
|
|
|
|
MUST_USE_UNIT,
|
|
|
|
DOUBLE_MUST_USE,
|
|
|
|
MUST_USE_CANDIDATE,
|
2020-10-23 20:16:59 +00:00
|
|
|
RESULT_UNIT_ERR,
|
2019-09-18 06:37:41 +00:00
|
|
|
]);
|
2016-03-08 23:48:10 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for Functions {
|
2016-12-21 09:25:14 +00:00
|
|
|
fn check_fn(
|
2016-12-21 11:14:54 +00:00
|
|
|
&mut self,
|
2020-06-25 20:41:36 +00:00
|
|
|
cx: &LateContext<'tcx>,
|
2016-12-21 11:14:54 +00:00
|
|
|
kind: intravisit::FnKind<'tcx>,
|
2019-12-30 04:02:10 +00:00
|
|
|
decl: &'tcx hir::FnDecl<'_>,
|
2019-12-22 14:42:41 +00:00
|
|
|
body: &'tcx hir::Body<'_>,
|
2016-12-21 11:14:54 +00:00
|
|
|
span: Span,
|
2019-02-20 10:11:11 +00:00
|
|
|
hir_id: hir::HirId,
|
2016-12-21 09:25:14 +00:00
|
|
|
) {
|
2016-06-07 14:55:55 +00:00
|
|
|
let unsafety = match kind {
|
2020-01-06 16:39:50 +00:00
|
|
|
intravisit::FnKind::ItemFn(_, _, hir::FnHeader { unsafety, .. }, _, _) => unsafety,
|
|
|
|
intravisit::FnKind::Method(_, sig, _, _) => sig.header.unsafety,
|
|
|
|
intravisit::FnKind::Closure(_) => return,
|
2016-06-07 14:55:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// don't warn for implementations, it's not their fault
|
2020-01-30 23:39:10 +00:00
|
|
|
if !is_trait_impl_item(cx, hir_id) {
|
2016-08-08 15:21:47 +00:00
|
|
|
// don't lint extern functions decls, it's not their fault either
|
|
|
|
match kind {
|
2020-01-06 16:39:50 +00:00
|
|
|
intravisit::FnKind::Method(
|
2018-11-27 20:14:15 +00:00
|
|
|
_,
|
2019-11-08 20:12:08 +00:00
|
|
|
&hir::FnSig {
|
2018-11-27 20:14:15 +00:00
|
|
|
header: hir::FnHeader { abi: Abi::Rust, .. },
|
|
|
|
..
|
|
|
|
},
|
|
|
|
_,
|
|
|
|
_,
|
|
|
|
)
|
2020-01-06 16:39:50 +00:00
|
|
|
| intravisit::FnKind::ItemFn(_, _, hir::FnHeader { abi: Abi::Rust, .. }, _, _) => {
|
2019-09-18 06:37:41 +00:00
|
|
|
self.check_arg_number(cx, decl, span.with_hi(decl.output.span().hi()))
|
2018-11-27 20:14:15 +00:00
|
|
|
},
|
2016-08-08 15:21:47 +00:00
|
|
|
_ => {},
|
|
|
|
}
|
2016-03-08 23:48:10 +00:00
|
|
|
}
|
|
|
|
|
2019-10-03 19:09:32 +00:00
|
|
|
Self::check_raw_ptr(cx, unsafety, decl, body, hir_id);
|
2019-02-23 17:29:30 +00:00
|
|
|
self.check_line_number(cx, span, body);
|
2016-03-08 23:48:10 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
|
2019-09-18 06:37:41 +00:00
|
|
|
let attr = must_use_attr(&item.attrs);
|
2019-11-08 20:12:08 +00:00
|
|
|
if let hir::ItemKind::Fn(ref sig, ref _generics, ref body_id) = item.kind {
|
2020-10-23 20:16:59 +00:00
|
|
|
let is_public = cx.access_levels.is_exported(item.hir_id);
|
|
|
|
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
|
|
|
|
if is_public {
|
|
|
|
check_result_unit_err(cx, &sig.decl, item.span, fn_header_span);
|
|
|
|
}
|
2019-09-18 06:37:41 +00:00
|
|
|
if let Some(attr) = attr {
|
2019-11-08 20:12:08 +00:00
|
|
|
check_needless_must_use(cx, &sig.decl, item.hir_id, item.span, fn_header_span, attr);
|
2019-09-18 06:37:41 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-10-23 20:16:59 +00:00
|
|
|
if is_public && !is_proc_macro(cx.sess(), &item.attrs) && attr_by_name(&item.attrs, "no_mangle").is_none() {
|
2019-09-18 06:37:41 +00:00
|
|
|
check_must_use_candidate(
|
|
|
|
cx,
|
2019-11-08 20:12:08 +00:00
|
|
|
&sig.decl,
|
2019-09-18 06:37:41 +00:00
|
|
|
cx.tcx.hir().body(*body_id),
|
|
|
|
item.span,
|
|
|
|
item.hir_id,
|
2019-11-08 20:12:08 +00:00
|
|
|
item.span.with_hi(sig.decl.output.span().hi()),
|
2019-09-18 06:37:41 +00:00
|
|
|
"this function could have a `#[must_use]` attribute",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::ImplItem<'_>) {
|
2020-03-16 15:00:16 +00:00
|
|
|
if let hir::ImplItemKind::Fn(ref sig, ref body_id) = item.kind {
|
2020-10-23 20:16:59 +00:00
|
|
|
let is_public = cx.access_levels.is_exported(item.hir_id);
|
|
|
|
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
|
|
|
|
if is_public && trait_ref_of_method(cx, item.hir_id).is_none() {
|
|
|
|
check_result_unit_err(cx, &sig.decl, item.span, fn_header_span);
|
|
|
|
}
|
2019-09-18 06:37:41 +00:00
|
|
|
let attr = must_use_attr(&item.attrs);
|
|
|
|
if let Some(attr) = attr {
|
|
|
|
check_needless_must_use(cx, &sig.decl, item.hir_id, item.span, fn_header_span, attr);
|
2020-10-23 20:16:59 +00:00
|
|
|
} else if is_public
|
2020-07-30 01:27:50 +00:00
|
|
|
&& !is_proc_macro(cx.sess(), &item.attrs)
|
2019-11-12 22:36:22 +00:00
|
|
|
&& trait_ref_of_method(cx, item.hir_id).is_none()
|
|
|
|
{
|
2019-09-18 06:37:41 +00:00
|
|
|
check_must_use_candidate(
|
|
|
|
cx,
|
|
|
|
&sig.decl,
|
|
|
|
cx.tcx.hir().body(*body_id),
|
|
|
|
item.span,
|
|
|
|
item.hir_id,
|
|
|
|
item.span.with_hi(sig.decl.output.span().hi()),
|
|
|
|
"this method could have a `#[must_use]` attribute",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::TraitItem<'_>) {
|
2020-03-12 19:56:55 +00:00
|
|
|
if let hir::TraitItemKind::Fn(ref sig, ref eid) = item.kind {
|
2016-08-08 15:21:47 +00:00
|
|
|
// don't lint extern functions decls, it's not their fault
|
2018-06-24 13:32:40 +00:00
|
|
|
if sig.header.abi == Abi::Rust {
|
2019-09-18 06:37:41 +00:00
|
|
|
self.check_arg_number(cx, &sig.decl, item.span.with_hi(sig.decl.output.span().hi()));
|
2016-08-08 15:21:47 +00:00
|
|
|
}
|
2020-10-23 20:16:59 +00:00
|
|
|
let is_public = cx.access_levels.is_exported(item.hir_id);
|
|
|
|
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
|
|
|
|
if is_public {
|
|
|
|
check_result_unit_err(cx, &sig.decl, item.span, fn_header_span);
|
|
|
|
}
|
2016-06-07 14:55:55 +00:00
|
|
|
|
2019-09-18 06:37:41 +00:00
|
|
|
let attr = must_use_attr(&item.attrs);
|
|
|
|
if let Some(attr) = attr {
|
|
|
|
check_needless_must_use(cx, &sig.decl, item.hir_id, item.span, fn_header_span, attr);
|
|
|
|
}
|
2020-03-16 15:00:16 +00:00
|
|
|
if let hir::TraitFn::Provided(eid) = *eid {
|
2018-12-08 00:56:03 +00:00
|
|
|
let body = cx.tcx.hir().body(eid);
|
2019-10-03 19:09:32 +00:00
|
|
|
Self::check_raw_ptr(cx, sig.header.unsafety, &sig.decl, body, item.hir_id);
|
2019-09-18 06:37:41 +00:00
|
|
|
|
2020-10-23 20:16:59 +00:00
|
|
|
if attr.is_none() && is_public && !is_proc_macro(cx.sess(), &item.attrs) {
|
2019-09-18 06:37:41 +00:00
|
|
|
check_must_use_candidate(
|
|
|
|
cx,
|
|
|
|
&sig.decl,
|
|
|
|
body,
|
|
|
|
item.span,
|
|
|
|
item.hir_id,
|
|
|
|
item.span.with_hi(sig.decl.output.span().hi()),
|
|
|
|
"this method could have a `#[must_use]` attribute",
|
|
|
|
);
|
|
|
|
}
|
2016-06-07 14:55:55 +00:00
|
|
|
}
|
2016-03-08 23:48:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> Functions {
|
|
|
|
fn check_arg_number(self, cx: &LateContext<'_>, decl: &hir::FnDecl<'_>, fn_span: Span) {
|
2016-03-08 23:48:10 +00:00
|
|
|
let args = decl.inputs.len() as u64;
|
|
|
|
if args > self.threshold {
|
2017-08-09 07:30:56 +00:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
TOO_MANY_ARGUMENTS,
|
2019-09-18 06:37:41 +00:00
|
|
|
fn_span,
|
2017-08-09 07:30:56 +00:00
|
|
|
&format!("this function has too many arguments ({}/{})", args, self.threshold),
|
|
|
|
);
|
2016-03-08 23:48:10 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-07 14:55:55 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_line_number(self, cx: &LateContext<'_>, span: Span, body: &'tcx hir::Body<'_>) {
|
2019-01-19 22:35:32 +00:00
|
|
|
if in_external_macro(cx.sess(), span) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-23 17:29:30 +00:00
|
|
|
let code_snippet = snippet(cx, body.value.span, "..");
|
2019-01-13 21:26:03 +00:00
|
|
|
let mut line_count: u64 = 0;
|
2019-01-13 15:19:02 +00:00
|
|
|
let mut in_comment = false;
|
2019-01-13 21:26:03 +00:00
|
|
|
let mut code_in_line;
|
|
|
|
|
|
|
|
// Skip the surrounding function decl.
|
2019-09-04 14:19:59 +00:00
|
|
|
let start_brace_idx = code_snippet.find('{').map_or(0, |i| i + 1);
|
|
|
|
let end_brace_idx = code_snippet.rfind('}').unwrap_or_else(|| code_snippet.len());
|
2019-01-13 21:26:03 +00:00
|
|
|
let function_lines = code_snippet[start_brace_idx..end_brace_idx].lines();
|
|
|
|
|
|
|
|
for mut line in function_lines {
|
|
|
|
code_in_line = false;
|
|
|
|
loop {
|
|
|
|
line = line.trim_start();
|
2019-01-13 21:53:26 +00:00
|
|
|
if line.is_empty() {
|
|
|
|
break;
|
|
|
|
}
|
2019-01-13 21:26:03 +00:00
|
|
|
if in_comment {
|
2019-01-13 15:19:02 +00:00
|
|
|
match line.find("*/") {
|
|
|
|
Some(i) => {
|
2019-01-13 21:26:03 +00:00
|
|
|
line = &line[i + 2..];
|
|
|
|
in_comment = false;
|
|
|
|
continue;
|
2019-01-13 15:19:02 +00:00
|
|
|
},
|
2019-01-13 21:53:26 +00:00
|
|
|
None => break,
|
2019-01-13 15:19:02 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-09-04 14:19:59 +00:00
|
|
|
let multi_idx = line.find("/*").unwrap_or_else(|| line.len());
|
|
|
|
let single_idx = line.find("//").unwrap_or_else(|| line.len());
|
2019-01-13 21:26:03 +00:00
|
|
|
code_in_line |= multi_idx > 0 && single_idx > 0;
|
|
|
|
// Implies multi_idx is below line.len()
|
|
|
|
if multi_idx < single_idx {
|
|
|
|
line = &line[multi_idx + 2..];
|
|
|
|
in_comment = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2019-01-13 15:19:02 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-13 21:53:26 +00:00
|
|
|
if code_in_line {
|
|
|
|
line_count += 1;
|
|
|
|
}
|
2019-01-13 15:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if line_count > self.max_lines {
|
2020-09-10 15:47:07 +00:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
TOO_MANY_LINES,
|
|
|
|
span,
|
|
|
|
&format!("this function has too many lines ({}/{})", line_count, self.max_lines),
|
|
|
|
)
|
2019-01-13 15:19:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 09:25:14 +00:00
|
|
|
fn check_raw_ptr(
|
2020-06-25 20:41:36 +00:00
|
|
|
cx: &LateContext<'tcx>,
|
2016-12-21 11:14:54 +00:00
|
|
|
unsafety: hir::Unsafety,
|
2019-12-30 04:02:10 +00:00
|
|
|
decl: &'tcx hir::FnDecl<'_>,
|
2019-12-22 14:42:41 +00:00
|
|
|
body: &'tcx hir::Body<'_>,
|
2019-02-27 09:39:33 +00:00
|
|
|
hir_id: hir::HirId,
|
2016-12-21 09:25:14 +00:00
|
|
|
) {
|
2017-01-04 21:46:41 +00:00
|
|
|
let expr = &body.value;
|
2019-03-23 22:37:38 +00:00
|
|
|
if unsafety == hir::Unsafety::Normal && cx.access_levels.is_exported(hir_id) {
|
2017-01-04 23:53:16 +00:00
|
|
|
let raw_ptrs = iter_input_pats(decl, body)
|
|
|
|
.zip(decl.inputs.iter())
|
|
|
|
.filter_map(|(arg, ty)| raw_ptr_arg(arg, ty))
|
2018-09-11 23:34:52 +00:00
|
|
|
.collect::<FxHashSet<_>>();
|
2016-06-07 14:55:55 +00:00
|
|
|
|
|
|
|
if !raw_ptrs.is_empty() {
|
2020-07-17 08:47:04 +00:00
|
|
|
let typeck_results = cx.tcx.typeck_body(body.id());
|
2016-06-07 14:55:55 +00:00
|
|
|
let mut v = DerefVisitor {
|
2018-03-15 15:07:15 +00:00
|
|
|
cx,
|
2016-06-07 14:55:55 +00:00
|
|
|
ptrs: raw_ptrs,
|
2020-07-17 08:47:04 +00:00
|
|
|
typeck_results,
|
2016-06-07 14:55:55 +00:00
|
|
|
};
|
|
|
|
|
2020-01-06 16:39:50 +00:00
|
|
|
intravisit::walk_expr(&mut v, expr);
|
2016-06-07 14:55:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-23 20:16:59 +00:00
|
|
|
fn check_result_unit_err(cx: &LateContext<'_>, decl: &hir::FnDecl<'_>, item_span: Span, fn_header_span: Span) {
|
|
|
|
if_chain! {
|
|
|
|
if !in_external_macro(cx.sess(), item_span);
|
|
|
|
if let hir::FnRetTy::Return(ref ty) = decl.output;
|
|
|
|
if let hir::TyKind::Path(ref qpath) = ty.kind;
|
|
|
|
if is_type_diagnostic_item(cx, hir_ty_to_ty(cx.tcx, ty), sym!(result_type));
|
|
|
|
if let Some(ref args) = last_path_segment(qpath).args;
|
|
|
|
if let [_, hir::GenericArg::Type(ref err_ty)] = args.args;
|
|
|
|
if let hir::TyKind::Tup(t) = err_ty.kind;
|
|
|
|
if t.is_empty();
|
|
|
|
then {
|
|
|
|
span_lint_and_help(
|
|
|
|
cx,
|
|
|
|
RESULT_UNIT_ERR,
|
|
|
|
fn_header_span,
|
|
|
|
"this returns a `Result<_, ()>",
|
|
|
|
None,
|
|
|
|
"use a custom Error type instead",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 06:37:41 +00:00
|
|
|
fn check_needless_must_use(
|
2020-06-25 20:41:36 +00:00
|
|
|
cx: &LateContext<'_>,
|
2019-12-30 04:02:10 +00:00
|
|
|
decl: &hir::FnDecl<'_>,
|
2019-09-18 06:37:41 +00:00
|
|
|
item_id: hir::HirId,
|
|
|
|
item_span: Span,
|
|
|
|
fn_header_span: Span,
|
|
|
|
attr: &Attribute,
|
|
|
|
) {
|
|
|
|
if in_external_macro(cx.sess(), item_span) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if returns_unit(decl) {
|
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
MUST_USE_UNIT,
|
|
|
|
fn_header_span,
|
|
|
|
"this unit-returning function has a `#[must_use]` attribute",
|
2020-04-17 06:08:00 +00:00
|
|
|
|diag| {
|
|
|
|
diag.span_suggestion(
|
2019-09-18 06:37:41 +00:00
|
|
|
attr.span,
|
|
|
|
"remove the attribute",
|
|
|
|
"".into(),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} else if !attr.is_value_str() && is_must_use_ty(cx, return_ty(cx, item_id)) {
|
2020-01-27 01:56:22 +00:00
|
|
|
span_lint_and_help(
|
2019-09-18 06:37:41 +00:00
|
|
|
cx,
|
|
|
|
DOUBLE_MUST_USE,
|
|
|
|
fn_header_span,
|
|
|
|
"this function has an empty `#[must_use]` attribute, but returns a type already marked as `#[must_use]`",
|
2020-04-18 10:28:29 +00:00
|
|
|
None,
|
2019-09-18 06:37:41 +00:00
|
|
|
"either add some descriptive text or remove the attribute",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_must_use_candidate<'tcx>(
|
|
|
|
cx: &LateContext<'tcx>,
|
2019-12-30 04:02:10 +00:00
|
|
|
decl: &'tcx hir::FnDecl<'_>,
|
2019-12-22 14:42:41 +00:00
|
|
|
body: &'tcx hir::Body<'_>,
|
2019-09-18 06:37:41 +00:00
|
|
|
item_span: Span,
|
|
|
|
item_id: hir::HirId,
|
|
|
|
fn_span: Span,
|
|
|
|
msg: &str,
|
|
|
|
) {
|
|
|
|
if has_mutable_arg(cx, body)
|
|
|
|
|| mutates_static(cx, body)
|
|
|
|
|| in_external_macro(cx.sess(), item_span)
|
|
|
|
|| returns_unit(decl)
|
2019-12-06 19:10:14 +00:00
|
|
|
|| !cx.access_levels.is_exported(item_id)
|
2019-09-18 06:37:41 +00:00
|
|
|
|| is_must_use_ty(cx, return_ty(cx, item_id))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2020-04-17 06:08:00 +00:00
|
|
|
span_lint_and_then(cx, MUST_USE_CANDIDATE, fn_span, msg, |diag| {
|
2019-09-18 06:37:41 +00:00
|
|
|
if let Some(snippet) = snippet_opt(cx, fn_span) {
|
2020-04-17 06:08:00 +00:00
|
|
|
diag.span_suggestion(
|
2019-09-18 06:37:41 +00:00
|
|
|
fn_span,
|
|
|
|
"add the attribute",
|
|
|
|
format!("#[must_use] {}", snippet),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-30 04:02:10 +00:00
|
|
|
fn returns_unit(decl: &hir::FnDecl<'_>) -> bool {
|
2019-09-18 06:37:41 +00:00
|
|
|
match decl.output {
|
2020-02-17 09:36:17 +00:00
|
|
|
hir::FnRetTy::DefaultReturn(_) => true,
|
|
|
|
hir::FnRetTy::Return(ref ty) => match ty.kind {
|
2019-09-18 06:37:41 +00:00
|
|
|
hir::TyKind::Tup(ref tys) => tys.is_empty(),
|
|
|
|
hir::TyKind::Never => true,
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn has_mutable_arg(cx: &LateContext<'_>, body: &hir::Body<'_>) -> bool {
|
2019-09-18 06:37:41 +00:00
|
|
|
let mut tys = FxHashSet::default();
|
|
|
|
body.params.iter().any(|param| is_mutable_pat(cx, ¶m.pat, &mut tys))
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn is_mutable_pat(cx: &LateContext<'_>, pat: &hir::Pat<'_>, tys: &mut FxHashSet<DefId>) -> bool {
|
2019-09-18 06:37:41 +00:00
|
|
|
if let hir::PatKind::Wild = pat.kind {
|
|
|
|
return false; // ignore `_` patterns
|
|
|
|
}
|
2020-03-19 13:33:10 +00:00
|
|
|
let def_id = pat.hir_id.owner.to_def_id();
|
2020-07-17 08:47:04 +00:00
|
|
|
if cx.tcx.has_typeck_results(def_id) {
|
|
|
|
is_mutable_ty(cx, &cx.tcx.typeck(def_id.expect_local()).pat_ty(pat), pat.span, tys)
|
2019-09-18 06:37:41 +00:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static KNOWN_WRAPPER_TYS: &[&[&str]] = &[&["alloc", "rc", "Rc"], &["std", "sync", "Arc"]];
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn is_mutable_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, span: Span, tys: &mut FxHashSet<DefId>) -> bool {
|
2020-08-03 22:18:29 +00:00
|
|
|
match *ty.kind() {
|
2019-09-18 06:37:41 +00:00
|
|
|
// primitive types are never mutable
|
2020-02-21 08:40:13 +00:00
|
|
|
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Str => false,
|
|
|
|
ty::Adt(ref adt, ref substs) => {
|
2020-06-21 09:20:48 +00:00
|
|
|
tys.insert(adt.did) && !ty.is_freeze(cx.tcx.at(span), cx.param_env)
|
2019-09-18 06:37:41 +00:00
|
|
|
|| KNOWN_WRAPPER_TYS.iter().any(|path| match_def_path(cx, adt.did, path))
|
|
|
|
&& substs.types().any(|ty| is_mutable_ty(cx, ty, span, tys))
|
|
|
|
},
|
2020-02-21 08:40:13 +00:00
|
|
|
ty::Tuple(ref substs) => substs.types().any(|ty| is_mutable_ty(cx, ty, span, tys)),
|
|
|
|
ty::Array(ty, _) | ty::Slice(ty) => is_mutable_ty(cx, ty, span, tys),
|
|
|
|
ty::RawPtr(ty::TypeAndMut { ty, mutbl }) | ty::Ref(_, ty, mutbl) => {
|
2019-12-21 18:38:45 +00:00
|
|
|
mutbl == hir::Mutability::Mut || is_mutable_ty(cx, ty, span, tys)
|
2019-09-18 06:37:41 +00:00
|
|
|
},
|
|
|
|
// calling something constitutes a side effect, so return true on all callables
|
|
|
|
// also never calls need not be used, so return true for them, too
|
|
|
|
_ => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-30 04:02:10 +00:00
|
|
|
fn raw_ptr_arg(arg: &hir::Param<'_>, ty: &hir::Ty<'_>) -> Option<hir::HirId> {
|
2019-09-27 15:16:06 +00:00
|
|
|
if let (&hir::PatKind::Binding(_, id, _, _), &hir::TyKind::Ptr(_)) = (&arg.pat.kind, &ty.kind) {
|
2017-09-12 12:26:40 +00:00
|
|
|
Some(id)
|
2016-06-07 14:55:55 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 18:36:23 +00:00
|
|
|
struct DerefVisitor<'a, 'tcx> {
|
2020-06-25 20:41:36 +00:00
|
|
|
cx: &'a LateContext<'tcx>,
|
2019-03-07 20:51:05 +00:00
|
|
|
ptrs: FxHashSet<hir::HirId>,
|
2020-07-17 08:47:04 +00:00
|
|
|
typeck_results: &'a ty::TypeckResults<'tcx>,
|
2016-06-07 14:55:55 +00:00
|
|
|
}
|
|
|
|
|
2019-09-18 06:37:41 +00:00
|
|
|
impl<'a, 'tcx> intravisit::Visitor<'tcx> for DerefVisitor<'a, 'tcx> {
|
2020-01-09 07:13:22 +00:00
|
|
|
type Map = Map<'tcx>;
|
|
|
|
|
2019-12-27 07:12:26 +00:00
|
|
|
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'_>) {
|
2019-09-27 15:16:06 +00:00
|
|
|
match expr.kind {
|
2019-12-27 07:12:26 +00:00
|
|
|
hir::ExprKind::Call(ref f, args) => {
|
2020-07-17 08:47:04 +00:00
|
|
|
let ty = self.typeck_results.expr_ty(f);
|
2016-06-07 15:29:22 +00:00
|
|
|
|
2017-06-29 13:38:25 +00:00
|
|
|
if type_is_unsafe_function(self.cx, ty) {
|
2016-06-07 15:29:22 +00:00
|
|
|
for arg in args {
|
|
|
|
self.check_arg(arg);
|
|
|
|
}
|
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2020-06-09 21:44:04 +00:00
|
|
|
hir::ExprKind::MethodCall(_, _, args, _) => {
|
2020-07-17 08:47:04 +00:00
|
|
|
let def_id = self.typeck_results.type_dependent_def_id(expr.hir_id).unwrap();
|
2017-06-02 04:13:04 +00:00
|
|
|
let base_type = self.cx.tcx.type_of(def_id);
|
2016-06-07 14:55:55 +00:00
|
|
|
|
2017-06-29 13:38:25 +00:00
|
|
|
if type_is_unsafe_function(self.cx, base_type) {
|
2016-06-07 15:29:22 +00:00
|
|
|
for arg in args {
|
|
|
|
self.check_arg(arg);
|
|
|
|
}
|
2016-06-07 14:55:55 +00:00
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2020-01-06 16:39:50 +00:00
|
|
|
hir::ExprKind::Unary(hir::UnOp::UnDeref, ref ptr) => self.check_arg(ptr),
|
2016-06-07 15:29:22 +00:00
|
|
|
_ => (),
|
2016-06-07 14:55:55 +00:00
|
|
|
}
|
|
|
|
|
2019-09-18 06:37:41 +00:00
|
|
|
intravisit::walk_expr(self, expr);
|
2016-06-07 14:55:55 +00:00
|
|
|
}
|
2019-09-18 06:37:41 +00:00
|
|
|
|
2020-03-15 22:41:20 +00:00
|
|
|
fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
|
2017-05-12 10:02:42 +00:00
|
|
|
intravisit::NestedVisitorMap::None
|
2016-12-06 10:32:21 +00:00
|
|
|
}
|
2016-03-08 23:48:10 +00:00
|
|
|
}
|
2016-06-07 15:29:22 +00:00
|
|
|
|
2019-06-19 18:36:23 +00:00
|
|
|
impl<'a, 'tcx> DerefVisitor<'a, 'tcx> {
|
2019-12-27 07:12:26 +00:00
|
|
|
fn check_arg(&self, ptr: &hir::Expr<'_>) {
|
2019-09-27 15:16:06 +00:00
|
|
|
if let hir::ExprKind::Path(ref qpath) = ptr.kind {
|
2019-09-18 17:29:04 +00:00
|
|
|
if let Res::Local(id) = qpath_res(self.cx, qpath, ptr.hir_id) {
|
2019-04-14 20:09:17 +00:00
|
|
|
if self.ptrs.contains(&id) {
|
2017-09-12 12:26:40 +00:00
|
|
|
span_lint(
|
|
|
|
self.cx,
|
|
|
|
NOT_UNSAFE_PTR_ARG_DEREF,
|
|
|
|
ptr.span,
|
|
|
|
"this public function dereferences a raw pointer but is not marked `unsafe`",
|
|
|
|
);
|
|
|
|
}
|
2016-06-07 15:29:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 06:37:41 +00:00
|
|
|
|
|
|
|
struct StaticMutVisitor<'a, 'tcx> {
|
2020-06-25 20:41:36 +00:00
|
|
|
cx: &'a LateContext<'tcx>,
|
2019-09-18 06:37:41 +00:00
|
|
|
mutates_static: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> intravisit::Visitor<'tcx> for StaticMutVisitor<'a, 'tcx> {
|
2020-01-09 07:13:22 +00:00
|
|
|
type Map = Map<'tcx>;
|
|
|
|
|
2019-12-27 07:12:26 +00:00
|
|
|
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'_>) {
|
2020-02-21 08:39:38 +00:00
|
|
|
use hir::ExprKind::{AddrOf, Assign, AssignOp, Call, MethodCall};
|
2019-09-18 06:37:41 +00:00
|
|
|
|
|
|
|
if self.mutates_static {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
match expr.kind {
|
2020-06-09 21:44:04 +00:00
|
|
|
Call(_, args) | MethodCall(_, _, args, _) => {
|
2019-09-18 06:37:41 +00:00
|
|
|
let mut tys = FxHashSet::default();
|
|
|
|
for arg in args {
|
2020-03-19 13:33:10 +00:00
|
|
|
let def_id = arg.hir_id.owner.to_def_id();
|
2020-07-17 08:47:04 +00:00
|
|
|
if self.cx.tcx.has_typeck_results(def_id)
|
2019-09-18 06:37:41 +00:00
|
|
|
&& is_mutable_ty(
|
|
|
|
self.cx,
|
2020-07-17 08:47:04 +00:00
|
|
|
self.cx.tcx.typeck(def_id.expect_local()).expr_ty(arg),
|
2019-09-18 06:37:41 +00:00
|
|
|
arg.span,
|
|
|
|
&mut tys,
|
|
|
|
)
|
|
|
|
&& is_mutated_static(self.cx, arg)
|
|
|
|
{
|
|
|
|
self.mutates_static = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
tys.clear();
|
|
|
|
}
|
|
|
|
},
|
2019-12-24 04:16:04 +00:00
|
|
|
Assign(ref target, ..) | AssignOp(_, ref target, _) | AddrOf(_, hir::Mutability::Mut, ref target) => {
|
2019-09-18 06:37:41 +00:00
|
|
|
self.mutates_static |= is_mutated_static(self.cx, target)
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-15 22:41:20 +00:00
|
|
|
fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
|
2019-09-18 06:37:41 +00:00
|
|
|
intravisit::NestedVisitorMap::None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn is_mutated_static(cx: &LateContext<'_>, e: &hir::Expr<'_>) -> bool {
|
2020-02-21 08:39:38 +00:00
|
|
|
use hir::ExprKind::{Field, Index, Path};
|
2019-09-18 06:37:41 +00:00
|
|
|
|
|
|
|
match e.kind {
|
2020-07-14 12:59:59 +00:00
|
|
|
Path(ref qpath) => !matches!(qpath_res(cx, qpath, e.hir_id), Res::Local(_)),
|
2019-09-18 06:37:41 +00:00
|
|
|
Field(ref inner, _) | Index(ref inner, _) => is_mutated_static(cx, inner),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn mutates_static<'tcx>(cx: &LateContext<'tcx>, body: &'tcx hir::Body<'_>) -> bool {
|
2019-09-18 06:37:41 +00:00
|
|
|
let mut v = StaticMutVisitor {
|
|
|
|
cx,
|
|
|
|
mutates_static: false,
|
|
|
|
};
|
|
|
|
intravisit::walk_expr(&mut v, &body.value);
|
|
|
|
v.mutates_static
|
|
|
|
}
|