-#![feature(bindings_after_at)]
#![feature(box_patterns)]
-#![feature(box_syntax)]
-#![feature(concat_idents)]
-#![feature(crate_visibility_modifier)]
-#![feature(drain_filter)]
#![feature(in_band_lifetimes)]
-#![feature(once_cell)]
#![feature(or_patterns)]
#![feature(rustc_private)]
-#![feature(stmt_expr_attributes)]
-#![feature(control_flow_enum)]
+#![recursion_limit = "512"]
+#![allow(clippy::missing_errors_doc, clippy::missing_panics_doc, clippy::must_use_candidate)]
+// FIXME: switch to something more ergonomic here, once available.
+// (Currently there is no way to opt into sysroot crates without `extern crate`.)
extern crate rustc_ast;
extern crate rustc_ast_pretty;
-extern crate rustc_attr;
extern crate rustc_data_structures;
-extern crate rustc_driver;
extern crate rustc_errors;
extern crate rustc_hir;
extern crate rustc_hir_pretty;
-extern crate rustc_index;
extern crate rustc_infer;
-extern crate rustc_lexer;
extern crate rustc_lint;
extern crate rustc_middle;
extern crate rustc_mir;
-extern crate rustc_parse;
-extern crate rustc_parse_format;
extern crate rustc_session;
extern crate rustc_span;
extern crate rustc_target;
#[allow(clippy::module_name_repetitions)]
pub mod ast_utils;
pub mod attrs;
-pub mod author;
pub mod camel_case;
pub mod comparisons;
-pub mod conf;
pub mod consts;
mod diagnostics;
pub mod eager_or_lazy;
pub mod higher;
mod hir_utils;
-pub mod inspector;
-#[cfg(feature = "internal-lints")]
-pub mod internal_lints;
pub mod numeric_literal;
pub mod paths;
pub mod ptr;
use crate::consts::{constant, Constant};
-/// Macro used to declare a Clippy lint.
-///
-/// Every lint declaration consists of 4 parts:
-///
-/// 1. The documentation, which is used for the website
-/// 2. The `LINT_NAME`. See [lint naming][lint_naming] on lint naming conventions.
-/// 3. The `lint_level`, which is a mapping from *one* of our lint groups to `Allow`, `Warn` or
-/// `Deny`. The lint level here has nothing to do with what lint groups the lint is a part of.
-/// 4. The `description` that contains a short explanation on what's wrong with code where the
-/// lint is triggered.
-///
-/// Currently the categories `style`, `correctness`, `complexity` and `perf` are enabled by default.
-/// As said in the README.md of this repository, if the lint level mapping changes, please update
-/// README.md.
-///
-/// # Example
-///
-/// ```
-/// #![feature(rustc_private)]
-/// extern crate rustc_session;
-/// use rustc_session::declare_tool_lint;
-/// use clippy_lints::declare_clippy_lint;
-///
-/// declare_clippy_lint! {
-/// /// **What it does:** Checks for ... (describe what the lint matches).
-/// ///
-/// /// **Why is this bad?** Supply the reason for linting the code.
-/// ///
-/// /// **Known problems:** None. (Or describe where it could go wrong.)
-/// ///
-/// /// **Example:**
-/// ///
-/// /// ```rust
-/// /// // Bad
-/// /// Insert a short example of code that triggers the lint
-/// ///
-/// /// // Good
-/// /// Insert a short example of improved code that doesn't trigger the lint
-/// /// ```
-/// pub LINT_NAME,
-/// pedantic,
-/// "description"
-/// }
-/// ```
-/// [lint_naming]: https://rust-lang.github.io/rfcs/0344-conventions-galore.html#lints
-#[macro_export]
-macro_rules! declare_clippy_lint {
- { $(#[$attr:meta])* pub $name:tt, style, $description:tt } => {
- declare_tool_lint! {
- $(#[$attr])* pub clippy::$name, Warn, $description, report_in_external_macro: true
- }
- };
- { $(#[$attr:meta])* pub $name:tt, correctness, $description:tt } => {
- declare_tool_lint! {
- $(#[$attr])* pub clippy::$name, Deny, $description, report_in_external_macro: true
- }
- };
- { $(#[$attr:meta])* pub $name:tt, complexity, $description:tt } => {
- declare_tool_lint! {
- $(#[$attr])* pub clippy::$name, Warn, $description, report_in_external_macro: true
- }
- };
- { $(#[$attr:meta])* pub $name:tt, perf, $description:tt } => {
- declare_tool_lint! {
- $(#[$attr])* pub clippy::$name, Warn, $description, report_in_external_macro: true
- }
- };
- { $(#[$attr:meta])* pub $name:tt, pedantic, $description:tt } => {
- declare_tool_lint! {
- $(#[$attr])* pub clippy::$name, Allow, $description, report_in_external_macro: true
- }
- };
- { $(#[$attr:meta])* pub $name:tt, restriction, $description:tt } => {
- declare_tool_lint! {
- $(#[$attr])* pub clippy::$name, Allow, $description, report_in_external_macro: true
- }
- };
- { $(#[$attr:meta])* pub $name:tt, cargo, $description:tt } => {
- declare_tool_lint! {
- $(#[$attr])* pub clippy::$name, Allow, $description, report_in_external_macro: true
- }
- };
- { $(#[$attr:meta])* pub $name:tt, nursery, $description:tt } => {
- declare_tool_lint! {
- $(#[$attr])* pub clippy::$name, Allow, $description, report_in_external_macro: true
- }
- };
- { $(#[$attr:meta])* pub $name:tt, internal, $description:tt } => {
- declare_tool_lint! {
- $(#[$attr])* pub clippy::$name, Allow, $description, report_in_external_macro: true
- }
- };
- { $(#[$attr:meta])* pub $name:tt, internal_warn, $description:tt } => {
- declare_tool_lint! {
- $(#[$attr])* pub clippy::$name, Warn, $description, report_in_external_macro: true
- }
- };
-}
-
pub fn parse_msrv(msrv: &str, sess: Option<&Session>, span: Option<Span>) -> Option<RustcVersion> {
if let Ok(version) = RustcVersion::parse(msrv) {
return Some(version);
trt_id.map_or(false, |trt_id| match_def_path(cx, trt_id, path))
}
+/// Checks if the method call given in `expr` belongs to a trait or other container with a given
+/// diagnostic item
+pub fn is_diagnostic_assoc_item(cx: &LateContext<'_>, def_id: DefId, diag_item: Symbol) -> bool {
+ cx.tcx
+ .opt_associated_item(def_id)
+ .and_then(|associated_item| match associated_item.container {
+ ty::TraitContainer(assoc_def_id) => Some(assoc_def_id),
+ ty::ImplContainer(assoc_def_id) => match cx.tcx.type_of(assoc_def_id).kind() {
+ ty::Adt(adt, _) => Some(adt.did),
+ ty::Slice(_) => cx.tcx.get_diagnostic_item(sym::slice), // this isn't perfect but it works
+ _ => None,
+ },
+ })
+ .map_or(false, |assoc_def_id| cx.tcx.is_diagnostic_item(diag_item, assoc_def_id))
+}
+
/// Checks if an expression references a variable of the given name.
pub fn match_var(expr: &Expr<'_>, var: Symbol) -> bool {
if let ExprKind::Path(QPath::Resolved(None, ref path)) = expr.kind {