2016-10-18 14:57:39 +00:00
|
|
|
//! checks for attributes
|
|
|
|
|
2018-05-30 08:15:50 +00:00
|
|
|
use crate::utils::get_attr;
|
2020-05-06 14:03:53 +00:00
|
|
|
use rustc_ast::ast::{Attribute, InlineAsmTemplatePiece};
|
2020-01-06 16:39:50 +00:00
|
|
|
use rustc_hir as hir;
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
2020-03-18 22:45:02 +00:00
|
|
|
use rustc_session::Session;
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2016-10-18 14:57:39 +00:00
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Dumps every ast/hir node which has the `#[clippy::dump]`
|
|
|
|
/// attribute
|
|
|
|
///
|
|
|
|
/// **Example:**
|
2019-08-02 06:13:54 +00:00
|
|
|
/// ```rust,ignore
|
2019-03-05 16:50:33 +00:00
|
|
|
/// #[clippy::dump]
|
|
|
|
/// extern crate foo;
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// prints
|
|
|
|
///
|
2019-08-02 06:13:54 +00:00
|
|
|
/// ```text
|
2019-03-05 16:50:33 +00:00
|
|
|
/// item `foo`
|
|
|
|
/// visibility inherited from outer item
|
|
|
|
/// extern crate dylib source: "/path/to/foo.so"
|
|
|
|
/// ```
|
2016-10-18 14:57:39 +00:00
|
|
|
pub DEEP_CODE_INSPECTION,
|
2018-03-29 11:41:53 +00:00
|
|
|
internal_warn,
|
2016-10-18 14:57:39 +00:00
|
|
|
"helper to dump info about code"
|
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
declare_lint_pass!(DeepCodeInspector => [DEEP_CODE_INSPECTION]);
|
2016-10-18 14:57:39 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for DeepCodeInspector {
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
|
2019-02-28 15:44:42 +00:00
|
|
|
if !has_attr(cx.sess(), &item.attrs) {
|
2016-10-18 14:57:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-10-24 12:29:09 +00:00
|
|
|
print_item(cx, item);
|
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::ImplItem<'_>) {
|
2019-02-28 15:44:42 +00:00
|
|
|
if !has_attr(cx.sess(), &item.attrs) {
|
2016-10-24 12:29:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-06-28 13:46:58 +00:00
|
|
|
println!("impl item `{}`", item.ident.name);
|
2018-07-02 17:07:12 +00:00
|
|
|
match item.vis.node {
|
|
|
|
hir::VisibilityKind::Public => println!("public"),
|
|
|
|
hir::VisibilityKind::Crate(_) => println!("visible crate wide"),
|
|
|
|
hir::VisibilityKind::Restricted { ref path, .. } => println!(
|
2017-09-05 09:33:04 +00:00
|
|
|
"visible in module `{}`",
|
2020-03-27 14:34:29 +00:00
|
|
|
rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_path(path, false))
|
2017-09-05 09:33:04 +00:00
|
|
|
),
|
2018-07-02 17:07:12 +00:00
|
|
|
hir::VisibilityKind::Inherited => println!("visibility inherited from outer item"),
|
2016-10-18 14:57:39 +00:00
|
|
|
}
|
2016-10-24 12:29:09 +00:00
|
|
|
if item.defaultness.is_default() {
|
|
|
|
println!("default");
|
|
|
|
}
|
2019-09-27 15:16:06 +00:00
|
|
|
match item.kind {
|
2017-01-04 22:18:11 +00:00
|
|
|
hir::ImplItemKind::Const(_, body_id) => {
|
2016-10-24 12:29:09 +00:00
|
|
|
println!("associated constant");
|
2018-12-08 00:56:03 +00:00
|
|
|
print_expr(cx, &cx.tcx.hir().body(body_id).value, 1);
|
2016-10-18 14:57:39 +00:00
|
|
|
},
|
2020-03-16 15:00:16 +00:00
|
|
|
hir::ImplItemKind::Fn(..) => println!("method"),
|
2019-08-05 05:30:01 +00:00
|
|
|
hir::ImplItemKind::TyAlias(_) => println!("associated type"),
|
2016-10-18 14:57:39 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-25 20:41:36 +00:00
|
|
|
// fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx
|
2017-08-09 07:30:56 +00:00
|
|
|
// hir::TraitItem) {
|
2016-12-20 17:21:30 +00:00
|
|
|
// if !has_attr(&item.attrs) {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
2020-06-25 20:41:36 +00:00
|
|
|
// fn check_variant(&mut self, cx: &LateContext<'tcx>, var: &'tcx
|
2017-08-09 07:30:56 +00:00
|
|
|
// hir::Variant, _:
|
2016-12-21 09:25:14 +00:00
|
|
|
// &hir::Generics) {
|
2016-12-20 17:21:30 +00:00
|
|
|
// if !has_attr(&var.node.attrs) {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
2020-06-25 20:41:36 +00:00
|
|
|
// fn check_struct_field(&mut self, cx: &LateContext<'tcx>, field: &'tcx
|
2017-08-09 07:30:56 +00:00
|
|
|
// hir::StructField) {
|
2016-12-20 17:21:30 +00:00
|
|
|
// if !has_attr(&field.attrs) {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
2016-10-18 14:57:39 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
|
2019-02-28 15:44:42 +00:00
|
|
|
if !has_attr(cx.sess(), &expr.attrs) {
|
2016-10-18 14:57:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-10-24 12:28:58 +00:00
|
|
|
print_expr(cx, expr, 0);
|
2016-10-18 14:57:39 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_arm(&mut self, cx: &LateContext<'tcx>, arm: &'tcx hir::Arm<'_>) {
|
2019-02-28 15:44:42 +00:00
|
|
|
if !has_attr(cx.sess(), &arm.attrs) {
|
2016-10-18 14:57:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-09-25 19:00:17 +00:00
|
|
|
print_pat(cx, &arm.pat, 1);
|
2016-10-24 12:28:58 +00:00
|
|
|
if let Some(ref guard) = arm.guard {
|
|
|
|
println!("guard:");
|
2018-09-02 07:38:25 +00:00
|
|
|
print_guard(cx, guard, 1);
|
2016-10-24 12:28:58 +00:00
|
|
|
}
|
|
|
|
println!("body:");
|
|
|
|
print_expr(cx, &arm.body, 1);
|
2016-10-18 14:57:39 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx hir::Stmt<'_>) {
|
Fix inconsistencies in handling of inert attributes on statements
When the 'early' and 'late' visitors visit an attribute target, they
activate any lint attributes (e.g. `#[allow]`) that apply to it.
This can affect warnings emitted on sibiling attributes. For example,
the following code does not produce an `unused_attributes` for
`#[inline]`, since the sibiling `#[allow(unused_attributes)]` suppressed
the warning.
```rust
trait Foo {
#[allow(unused_attributes)] #[inline] fn first();
#[inline] #[allow(unused_attributes)] fn second();
}
```
However, we do not do this for statements - instead, the lint attributes
only become active when we visit the struct nested inside `StmtKind`
(e.g. `Item`).
Currently, this is difficult to observe due to another issue - the
`HasAttrs` impl for `StmtKind` ignores attributes for `StmtKind::Item`.
As a result, the `unused_doc_comments` lint will never see attributes on
item statements.
This commit makes two interrelated fixes to the handling of inert
(non-proc-macro) attributes on statements:
* The `HasAttr` impl for `StmtKind` now returns attributes for
`StmtKind::Item`, treating it just like every other `StmtKind`
variant. The only place relying on the old behavior was macro
which has been updated to explicitly ignore attributes on item
statements. This allows the `unused_doc_comments` lint to fire for
item statements.
* The `early` and `late` lint visitors now activate lint attributes when
invoking the callback for `Stmt`. This ensures that a lint
attribute (e.g. `#[allow(unused_doc_comments)]`) can be applied to
sibiling attributes on an item statement.
For now, the `unused_doc_comments` lint is explicitly disabled on item
statements, which preserves the current behavior. The exact locatiosn
where this lint should fire are being discussed in PR #78306
2020-10-23 22:17:00 +00:00
|
|
|
if !has_attr(cx.sess(), stmt.kind.attrs(|id| cx.tcx.hir().item(id.id))) {
|
2016-10-18 14:57:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-09-27 15:16:06 +00:00
|
|
|
match stmt.kind {
|
2019-01-20 10:21:30 +00:00
|
|
|
hir::StmtKind::Local(ref local) => {
|
2020-07-17 08:47:04 +00:00
|
|
|
println!("local variable of type {}", cx.typeck_results().node_type(local.hir_id));
|
2019-01-20 10:21:30 +00:00
|
|
|
println!("pattern:");
|
|
|
|
print_pat(cx, &local.pat, 0);
|
|
|
|
if let Some(ref e) = local.init {
|
|
|
|
println!("init expression:");
|
|
|
|
print_expr(cx, e, 0);
|
|
|
|
}
|
2019-01-20 10:49:45 +00:00
|
|
|
},
|
2019-01-20 10:21:30 +00:00
|
|
|
hir::StmtKind::Item(_) => println!("item decl"),
|
|
|
|
hir::StmtKind::Expr(ref e) | hir::StmtKind::Semi(ref e) => print_expr(cx, e, 0),
|
2016-10-18 14:57:39 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-25 20:41:36 +00:00
|
|
|
// fn check_foreign_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx
|
2017-08-09 07:30:56 +00:00
|
|
|
// hir::ForeignItem) {
|
2016-12-20 17:21:30 +00:00
|
|
|
// if !has_attr(&item.attrs) {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
2016-10-18 14:57:39 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 15:44:42 +00:00
|
|
|
fn has_attr(sess: &Session, attrs: &[Attribute]) -> bool {
|
|
|
|
get_attr(sess, attrs, "dump").count() > 0
|
2016-10-18 14:57:39 +00:00
|
|
|
}
|
2016-10-24 12:28:58 +00:00
|
|
|
|
2018-10-11 22:18:58 +00:00
|
|
|
#[allow(clippy::similar_names)]
|
2019-09-03 04:26:49 +00:00
|
|
|
#[allow(clippy::too_many_lines)]
|
2020-06-25 20:41:36 +00:00
|
|
|
fn print_expr(cx: &LateContext<'_>, expr: &hir::Expr<'_>, indent: usize) {
|
2016-10-24 12:28:58 +00:00
|
|
|
let ind = " ".repeat(indent);
|
|
|
|
println!("{}+", ind);
|
2020-07-17 08:47:04 +00:00
|
|
|
println!("{}ty: {}", ind, cx.typeck_results().expr_ty(expr));
|
|
|
|
println!(
|
|
|
|
"{}adjustments: {:?}",
|
|
|
|
ind,
|
|
|
|
cx.typeck_results().adjustments().get(expr.hir_id)
|
|
|
|
);
|
2019-09-27 15:16:06 +00:00
|
|
|
match expr.kind {
|
2018-07-12 07:30:57 +00:00
|
|
|
hir::ExprKind::Box(ref e) => {
|
2017-05-05 12:25:54 +00:00
|
|
|
println!("{}Box", ind);
|
2016-10-24 12:28:58 +00:00
|
|
|
print_expr(cx, e, indent + 1);
|
|
|
|
},
|
2019-12-27 07:12:26 +00:00
|
|
|
hir::ExprKind::Array(v) => {
|
2017-05-05 12:25:54 +00:00
|
|
|
println!("{}Array", ind);
|
2016-10-24 12:28:58 +00:00
|
|
|
for e in v {
|
|
|
|
print_expr(cx, e, indent + 1);
|
|
|
|
}
|
|
|
|
},
|
2019-12-27 07:12:26 +00:00
|
|
|
hir::ExprKind::Call(ref func, args) => {
|
2017-05-05 12:25:54 +00:00
|
|
|
println!("{}Call", ind);
|
2016-10-24 12:28:58 +00:00
|
|
|
println!("{}function:", ind);
|
|
|
|
print_expr(cx, func, indent + 1);
|
|
|
|
println!("{}arguments:", ind);
|
|
|
|
for arg in args {
|
|
|
|
print_expr(cx, arg, indent + 1);
|
|
|
|
}
|
|
|
|
},
|
2020-06-09 21:44:04 +00:00
|
|
|
hir::ExprKind::MethodCall(ref path, _, args, _) => {
|
2017-05-05 12:25:54 +00:00
|
|
|
println!("{}MethodCall", ind);
|
2018-06-28 13:46:58 +00:00
|
|
|
println!("{}method name: {}", ind, path.ident.name);
|
2016-10-24 12:28:58 +00:00
|
|
|
for arg in args {
|
|
|
|
print_expr(cx, arg, indent + 1);
|
|
|
|
}
|
|
|
|
},
|
2019-12-27 07:12:26 +00:00
|
|
|
hir::ExprKind::Tup(v) => {
|
2017-05-05 12:25:54 +00:00
|
|
|
println!("{}Tup", ind);
|
2016-10-24 12:28:58 +00:00
|
|
|
for e in v {
|
|
|
|
print_expr(cx, e, indent + 1);
|
|
|
|
}
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
hir::ExprKind::Binary(op, ref lhs, ref rhs) => {
|
2017-05-05 12:25:54 +00:00
|
|
|
println!("{}Binary", ind);
|
2016-10-24 12:28:58 +00:00
|
|
|
println!("{}op: {:?}", ind, op.node);
|
|
|
|
println!("{}lhs:", ind);
|
|
|
|
print_expr(cx, lhs, indent + 1);
|
|
|
|
println!("{}rhs:", ind);
|
|
|
|
print_expr(cx, rhs, indent + 1);
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
hir::ExprKind::Unary(op, ref inner) => {
|
2017-05-05 12:25:54 +00:00
|
|
|
println!("{}Unary", ind);
|
2016-10-24 12:28:58 +00:00
|
|
|
println!("{}op: {:?}", ind, op);
|
|
|
|
print_expr(cx, inner, indent + 1);
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
hir::ExprKind::Lit(ref lit) => {
|
2017-05-05 12:25:54 +00:00
|
|
|
println!("{}Lit", ind);
|
2016-10-24 12:28:58 +00:00
|
|
|
println!("{}{:?}", ind, lit);
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
hir::ExprKind::Cast(ref e, ref target) => {
|
2017-05-05 12:25:54 +00:00
|
|
|
println!("{}Cast", ind);
|
2016-10-24 12:28:58 +00:00
|
|
|
print_expr(cx, e, indent + 1);
|
|
|
|
println!("{}target type: {:?}", ind, target);
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
hir::ExprKind::Type(ref e, ref target) => {
|
2017-05-05 12:25:54 +00:00
|
|
|
println!("{}Type", ind);
|
2016-10-24 12:28:58 +00:00
|
|
|
print_expr(cx, e, indent + 1);
|
|
|
|
println!("{}target type: {:?}", ind, target);
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
hir::ExprKind::Loop(..) => {
|
2017-05-05 12:25:54 +00:00
|
|
|
println!("{}Loop", ind);
|
2016-10-24 12:28:58 +00:00
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
hir::ExprKind::Match(ref cond, _, ref source) => {
|
2017-05-05 12:25:54 +00:00
|
|
|
println!("{}Match", ind);
|
2016-10-24 12:28:58 +00:00
|
|
|
println!("{}condition:", ind);
|
|
|
|
print_expr(cx, cond, indent + 1);
|
|
|
|
println!("{}source: {:?}", ind, source);
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
hir::ExprKind::Closure(ref clause, _, _, _, _) => {
|
2017-05-05 12:25:54 +00:00
|
|
|
println!("{}Closure", ind);
|
2016-10-24 12:28:58 +00:00
|
|
|
println!("{}clause: {:?}", ind, clause);
|
|
|
|
},
|
2019-06-19 09:59:25 +00:00
|
|
|
hir::ExprKind::Yield(ref sub, _) => {
|
2017-08-30 08:54:24 +00:00
|
|
|
println!("{}Yield", ind);
|
|
|
|
print_expr(cx, sub, indent + 1);
|
2017-09-03 21:15:15 +00:00
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
hir::ExprKind::Block(_, _) => {
|
2017-05-05 12:25:54 +00:00
|
|
|
println!("{}Block", ind);
|
2016-10-24 12:28:58 +00:00
|
|
|
},
|
2019-12-24 04:16:04 +00:00
|
|
|
hir::ExprKind::Assign(ref lhs, ref rhs, _) => {
|
2017-05-05 12:25:54 +00:00
|
|
|
println!("{}Assign", ind);
|
2016-10-24 12:28:58 +00:00
|
|
|
println!("{}lhs:", ind);
|
|
|
|
print_expr(cx, lhs, indent + 1);
|
|
|
|
println!("{}rhs:", ind);
|
|
|
|
print_expr(cx, rhs, indent + 1);
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
hir::ExprKind::AssignOp(ref binop, ref lhs, ref rhs) => {
|
2017-05-05 12:25:54 +00:00
|
|
|
println!("{}AssignOp", ind);
|
2016-10-24 12:28:58 +00:00
|
|
|
println!("{}op: {:?}", ind, binop.node);
|
|
|
|
println!("{}lhs:", ind);
|
|
|
|
print_expr(cx, lhs, indent + 1);
|
|
|
|
println!("{}rhs:", ind);
|
|
|
|
print_expr(cx, rhs, indent + 1);
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
hir::ExprKind::Field(ref e, ident) => {
|
2017-05-05 12:25:54 +00:00
|
|
|
println!("{}Field", ind);
|
2018-05-29 08:56:58 +00:00
|
|
|
println!("{}field name: {}", ind, ident.name);
|
2016-10-24 12:28:58 +00:00
|
|
|
println!("{}struct expr:", ind);
|
|
|
|
print_expr(cx, e, indent + 1);
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
hir::ExprKind::Index(ref arr, ref idx) => {
|
2017-05-05 12:25:54 +00:00
|
|
|
println!("{}Index", ind);
|
2016-10-24 12:28:58 +00:00
|
|
|
println!("{}array expr:", ind);
|
|
|
|
print_expr(cx, arr, indent + 1);
|
|
|
|
println!("{}index expr:", ind);
|
|
|
|
print_expr(cx, idx, indent + 1);
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
hir::ExprKind::Path(hir::QPath::Resolved(ref ty, ref path)) => {
|
2016-12-01 21:31:56 +00:00
|
|
|
println!("{}Resolved Path, {:?}", ind, ty);
|
2016-10-24 12:28:58 +00:00
|
|
|
println!("{}path: {:?}", ind, path);
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
hir::ExprKind::Path(hir::QPath::TypeRelative(ref ty, ref seg)) => {
|
2016-12-01 21:31:56 +00:00
|
|
|
println!("{}Relative Path, {:?}", ind, ty);
|
|
|
|
println!("{}seg: {:?}", ind, seg);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2020-08-04 13:24:13 +00:00
|
|
|
hir::ExprKind::Path(hir::QPath::LangItem(lang_item, ..)) => {
|
|
|
|
println!("{}Lang Item Path, {:?}", ind, lang_item.name());
|
|
|
|
},
|
2019-11-27 22:34:32 +00:00
|
|
|
hir::ExprKind::AddrOf(kind, ref muta, ref e) => {
|
2017-05-05 12:25:54 +00:00
|
|
|
println!("{}AddrOf", ind);
|
2019-11-27 22:34:32 +00:00
|
|
|
println!("kind: {:?}", kind);
|
2016-10-24 12:28:58 +00:00
|
|
|
println!("mutability: {:?}", muta);
|
|
|
|
print_expr(cx, e, indent + 1);
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
hir::ExprKind::Break(_, ref e) => {
|
2017-05-05 12:25:54 +00:00
|
|
|
println!("{}Break", ind);
|
2016-11-25 18:24:55 +00:00
|
|
|
if let Some(ref e) = *e {
|
|
|
|
print_expr(cx, e, indent + 1);
|
|
|
|
}
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
hir::ExprKind::Continue(_) => println!("{}Again", ind),
|
|
|
|
hir::ExprKind::Ret(ref e) => {
|
2017-05-05 12:25:54 +00:00
|
|
|
println!("{}Ret", ind);
|
2016-10-24 12:28:58 +00:00
|
|
|
if let Some(ref e) = *e {
|
|
|
|
print_expr(cx, e, indent + 1);
|
|
|
|
}
|
|
|
|
},
|
2020-05-06 14:03:53 +00:00
|
|
|
hir::ExprKind::InlineAsm(ref asm) => {
|
|
|
|
println!("{}InlineAsm", ind);
|
|
|
|
println!("{}template: {}", ind, InlineAsmTemplatePiece::to_string(asm.template));
|
|
|
|
println!("{}options: {:?}", ind, asm.options);
|
|
|
|
println!("{}operands:", ind);
|
|
|
|
for op in asm.operands {
|
|
|
|
match op {
|
2020-05-28 13:45:24 +00:00
|
|
|
hir::InlineAsmOperand::In { expr, .. }
|
|
|
|
| hir::InlineAsmOperand::InOut { expr, .. }
|
|
|
|
| hir::InlineAsmOperand::Const { expr }
|
|
|
|
| hir::InlineAsmOperand::Sym { expr } => print_expr(cx, expr, indent + 1),
|
2020-05-06 14:03:53 +00:00
|
|
|
hir::InlineAsmOperand::Out { expr, .. } => {
|
|
|
|
if let Some(expr) = expr {
|
|
|
|
print_expr(cx, expr, indent + 1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
hir::InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
|
|
|
|
print_expr(cx, in_expr, indent + 1);
|
|
|
|
if let Some(out_expr) = out_expr {
|
|
|
|
print_expr(cx, out_expr, indent + 1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-03-27 14:41:56 +00:00
|
|
|
hir::ExprKind::LlvmInlineAsm(ref asm) => {
|
2019-11-22 12:25:38 +00:00
|
|
|
let inputs = &asm.inputs_exprs;
|
|
|
|
let outputs = &asm.outputs_exprs;
|
2020-03-27 14:41:56 +00:00
|
|
|
println!("{}LlvmInlineAsm", ind);
|
2016-10-24 12:28:58 +00:00
|
|
|
println!("{}inputs:", ind);
|
2019-11-22 12:25:38 +00:00
|
|
|
for e in inputs.iter() {
|
2016-10-24 12:28:58 +00:00
|
|
|
print_expr(cx, e, indent + 1);
|
|
|
|
}
|
|
|
|
println!("{}outputs:", ind);
|
2019-11-22 12:25:38 +00:00
|
|
|
for e in outputs.iter() {
|
2016-10-24 12:28:58 +00:00
|
|
|
print_expr(cx, e, indent + 1);
|
|
|
|
}
|
|
|
|
},
|
2019-12-27 07:12:26 +00:00
|
|
|
hir::ExprKind::Struct(ref path, fields, ref base) => {
|
2017-05-05 12:25:54 +00:00
|
|
|
println!("{}Struct", ind);
|
2016-10-24 12:28:58 +00:00
|
|
|
println!("{}path: {:?}", ind, path);
|
|
|
|
for field in fields {
|
2018-05-29 08:56:58 +00:00
|
|
|
println!("{}field \"{}\":", ind, field.ident.name);
|
2016-10-24 12:28:58 +00:00
|
|
|
print_expr(cx, &field.expr, indent + 1);
|
|
|
|
}
|
|
|
|
if let Some(ref base) = *base {
|
|
|
|
println!("{}base:", ind);
|
|
|
|
print_expr(cx, base, indent + 1);
|
|
|
|
}
|
|
|
|
},
|
2020-10-06 21:51:31 +00:00
|
|
|
hir::ExprKind::ConstBlock(ref anon_const) => {
|
|
|
|
println!("{}ConstBlock", ind);
|
|
|
|
println!("{}anon_const:", ind);
|
|
|
|
print_expr(cx, &cx.tcx.hir().body(anon_const.body).value, indent + 1);
|
|
|
|
},
|
2018-07-12 07:30:57 +00:00
|
|
|
hir::ExprKind::Repeat(ref val, ref anon_const) => {
|
2017-05-05 12:25:54 +00:00
|
|
|
println!("{}Repeat", ind);
|
2016-10-24 12:28:58 +00:00
|
|
|
println!("{}value:", ind);
|
|
|
|
print_expr(cx, val, indent + 1);
|
|
|
|
println!("{}repeat count:", ind);
|
2018-12-08 00:56:03 +00:00
|
|
|
print_expr(cx, &cx.tcx.hir().body(anon_const.body).value, indent + 1);
|
2016-10-24 12:28:58 +00:00
|
|
|
},
|
2018-12-28 11:29:34 +00:00
|
|
|
hir::ExprKind::Err => {
|
|
|
|
println!("{}Err", ind);
|
|
|
|
},
|
2019-05-01 20:18:05 +00:00
|
|
|
hir::ExprKind::DropTemps(ref e) => {
|
2019-05-01 20:52:19 +00:00
|
|
|
println!("{}DropTemps", ind);
|
2019-04-28 07:11:20 +00:00
|
|
|
print_expr(cx, e, indent + 1);
|
|
|
|
},
|
2016-10-24 12:28:58 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-24 12:29:09 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn print_item(cx: &LateContext<'_>, item: &hir::Item<'_>) {
|
2019-07-06 03:52:51 +00:00
|
|
|
let did = cx.tcx.hir().local_def_id(item.hir_id);
|
2018-12-30 00:09:24 +00:00
|
|
|
println!("item `{}`", item.ident.name);
|
2018-07-02 17:07:12 +00:00
|
|
|
match item.vis.node {
|
|
|
|
hir::VisibilityKind::Public => println!("public"),
|
|
|
|
hir::VisibilityKind::Crate(_) => println!("visible crate wide"),
|
|
|
|
hir::VisibilityKind::Restricted { ref path, .. } => println!(
|
2017-09-05 09:33:04 +00:00
|
|
|
"visible in module `{}`",
|
2020-03-27 14:34:29 +00:00
|
|
|
rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_path(path, false))
|
2017-09-05 09:33:04 +00:00
|
|
|
),
|
2018-07-02 17:07:12 +00:00
|
|
|
hir::VisibilityKind::Inherited => println!("visibility inherited from outer item"),
|
2016-10-24 12:29:09 +00:00
|
|
|
}
|
2019-09-27 15:16:06 +00:00
|
|
|
match item.kind {
|
2018-07-16 13:07:39 +00:00
|
|
|
hir::ItemKind::ExternCrate(ref _renamed_from) => {
|
2019-07-06 03:52:51 +00:00
|
|
|
let def_id = cx.tcx.hir().local_def_id(item.hir_id);
|
2017-09-12 12:26:40 +00:00
|
|
|
if let Some(crate_id) = cx.tcx.extern_mod_stmt_cnum(def_id) {
|
2017-09-09 05:23:08 +00:00
|
|
|
let source = cx.tcx.used_crate_source(crate_id);
|
|
|
|
if let Some(ref src) = source.dylib {
|
2016-10-24 12:29:09 +00:00
|
|
|
println!("extern crate dylib source: {:?}", src.0);
|
|
|
|
}
|
2017-09-09 05:23:08 +00:00
|
|
|
if let Some(ref src) = source.rlib {
|
2016-10-24 12:29:09 +00:00
|
|
|
println!("extern crate rlib source: {:?}", src.0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
println!("weird extern crate without a crate id");
|
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2018-07-16 13:07:39 +00:00
|
|
|
hir::ItemKind::Use(ref path, ref kind) => println!("{:?}, {:?}", path, kind),
|
|
|
|
hir::ItemKind::Static(..) => println!("static item of type {:#?}", cx.tcx.type_of(did)),
|
|
|
|
hir::ItemKind::Const(..) => println!("const item of type {:#?}", cx.tcx.type_of(did)),
|
|
|
|
hir::ItemKind::Fn(..) => {
|
2017-04-27 12:00:35 +00:00
|
|
|
let item_ty = cx.tcx.type_of(did);
|
2016-11-18 12:21:07 +00:00
|
|
|
println!("function of type {:#?}", item_ty);
|
2016-10-24 12:29:09 +00:00
|
|
|
},
|
2018-07-16 13:07:39 +00:00
|
|
|
hir::ItemKind::Mod(..) => println!("module"),
|
2020-11-11 21:40:09 +00:00
|
|
|
hir::ItemKind::ForeignMod { abi, .. } => println!("foreign module with abi: {}", abi),
|
2018-07-16 13:07:39 +00:00
|
|
|
hir::ItemKind::GlobalAsm(ref asm) => println!("global asm: {:?}", asm),
|
2019-08-05 05:30:01 +00:00
|
|
|
hir::ItemKind::TyAlias(..) => {
|
2017-04-27 12:00:35 +00:00
|
|
|
println!("type alias for {:?}", cx.tcx.type_of(did));
|
2016-10-24 12:29:09 +00:00
|
|
|
},
|
2019-08-03 06:44:32 +00:00
|
|
|
hir::ItemKind::OpaqueTy(..) => {
|
2018-06-19 07:56:37 +00:00
|
|
|
println!("existential type with real type {:?}", cx.tcx.type_of(did));
|
|
|
|
},
|
2018-07-16 13:07:39 +00:00
|
|
|
hir::ItemKind::Enum(..) => {
|
2017-04-27 12:00:35 +00:00
|
|
|
println!("enum definition of type {:?}", cx.tcx.type_of(did));
|
2016-10-24 12:29:09 +00:00
|
|
|
},
|
2018-07-16 13:07:39 +00:00
|
|
|
hir::ItemKind::Struct(..) => {
|
2017-04-27 12:00:35 +00:00
|
|
|
println!("struct definition of type {:?}", cx.tcx.type_of(did));
|
2016-10-24 12:29:09 +00:00
|
|
|
},
|
2018-07-16 13:07:39 +00:00
|
|
|
hir::ItemKind::Union(..) => {
|
2017-04-27 12:00:35 +00:00
|
|
|
println!("union definition of type {:?}", cx.tcx.type_of(did));
|
2016-10-24 12:29:09 +00:00
|
|
|
},
|
2018-07-16 13:07:39 +00:00
|
|
|
hir::ItemKind::Trait(..) => {
|
2016-10-24 12:29:09 +00:00
|
|
|
println!("trait decl");
|
2020-04-24 09:57:34 +00:00
|
|
|
if cx.tcx.trait_is_auto(did.to_def_id()) {
|
2017-11-05 12:19:11 +00:00
|
|
|
println!("trait is auto");
|
2016-10-24 12:29:09 +00:00
|
|
|
} else {
|
2017-11-05 12:19:11 +00:00
|
|
|
println!("trait is not auto");
|
2016-10-24 12:29:09 +00:00
|
|
|
}
|
|
|
|
},
|
2018-07-16 13:07:39 +00:00
|
|
|
hir::ItemKind::TraitAlias(..) => {
|
2017-12-15 09:02:39 +00:00
|
|
|
println!("trait alias");
|
2018-11-27 20:14:15 +00:00
|
|
|
},
|
2020-01-18 05:14:36 +00:00
|
|
|
hir::ItemKind::Impl {
|
|
|
|
of_trait: Some(ref _trait_ref),
|
|
|
|
..
|
|
|
|
} => {
|
2016-12-01 21:31:56 +00:00
|
|
|
println!("trait impl");
|
2016-10-24 12:29:09 +00:00
|
|
|
},
|
2020-01-18 05:14:36 +00:00
|
|
|
hir::ItemKind::Impl { of_trait: None, .. } => {
|
2016-10-24 12:29:09 +00:00
|
|
|
println!("impl");
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-11 22:18:58 +00:00
|
|
|
#[allow(clippy::similar_names)]
|
2019-09-03 04:26:49 +00:00
|
|
|
#[allow(clippy::too_many_lines)]
|
2020-06-25 20:41:36 +00:00
|
|
|
fn print_pat(cx: &LateContext<'_>, pat: &hir::Pat<'_>, indent: usize) {
|
2016-10-24 12:28:58 +00:00
|
|
|
let ind = " ".repeat(indent);
|
|
|
|
println!("{}+", ind);
|
2019-09-27 15:16:06 +00:00
|
|
|
match pat.kind {
|
2016-10-24 12:28:58 +00:00
|
|
|
hir::PatKind::Wild => println!("{}Wild", ind),
|
2019-02-03 07:12:07 +00:00
|
|
|
hir::PatKind::Binding(ref mode, .., ident, ref inner) => {
|
2016-10-24 12:28:58 +00:00
|
|
|
println!("{}Binding", ind);
|
|
|
|
println!("{}mode: {:?}", ind, mode);
|
2018-06-28 13:46:58 +00:00
|
|
|
println!("{}name: {}", ind, ident.name);
|
2016-10-24 12:28:58 +00:00
|
|
|
if let Some(ref inner) = *inner {
|
|
|
|
println!("{}inner:", ind);
|
|
|
|
print_pat(cx, inner, indent + 1);
|
|
|
|
}
|
|
|
|
},
|
2019-12-27 07:12:26 +00:00
|
|
|
hir::PatKind::Or(fields) => {
|
2019-08-18 11:14:47 +00:00
|
|
|
println!("{}Or", ind);
|
|
|
|
for field in fields {
|
|
|
|
print_pat(cx, field, indent + 1);
|
|
|
|
}
|
|
|
|
},
|
2019-12-27 07:12:26 +00:00
|
|
|
hir::PatKind::Struct(ref path, fields, ignore) => {
|
2016-10-24 12:28:58 +00:00
|
|
|
println!("{}Struct", ind);
|
2017-08-09 07:30:56 +00:00
|
|
|
println!(
|
|
|
|
"{}name: {}",
|
|
|
|
ind,
|
2020-03-27 14:34:29 +00:00
|
|
|
rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false))
|
2017-08-09 07:30:56 +00:00
|
|
|
);
|
2016-10-24 12:28:58 +00:00
|
|
|
println!("{}ignore leftover fields: {}", ind, ignore);
|
|
|
|
println!("{}fields:", ind);
|
|
|
|
for field in fields {
|
2019-08-16 02:30:38 +00:00
|
|
|
println!("{} field name: {}", ind, field.ident.name);
|
|
|
|
if field.is_shorthand {
|
2016-10-24 12:28:58 +00:00
|
|
|
println!("{} in shorthand notation", ind);
|
|
|
|
}
|
2019-08-16 02:30:38 +00:00
|
|
|
print_pat(cx, &field.pat, indent + 1);
|
2016-10-24 12:28:58 +00:00
|
|
|
}
|
|
|
|
},
|
2019-12-27 07:12:26 +00:00
|
|
|
hir::PatKind::TupleStruct(ref path, fields, opt_dots_position) => {
|
2016-10-24 12:28:58 +00:00
|
|
|
println!("{}TupleStruct", ind);
|
2017-08-09 07:30:56 +00:00
|
|
|
println!(
|
|
|
|
"{}path: {}",
|
|
|
|
ind,
|
2020-03-27 14:34:29 +00:00
|
|
|
rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false))
|
2017-08-09 07:30:56 +00:00
|
|
|
);
|
2016-10-24 12:28:58 +00:00
|
|
|
if let Some(dot_position) = opt_dots_position {
|
|
|
|
println!("{}dot position: {}", ind, dot_position);
|
|
|
|
}
|
2016-10-24 13:16:21 +00:00
|
|
|
for field in fields {
|
|
|
|
print_pat(cx, field, indent + 1);
|
2016-10-24 12:28:58 +00:00
|
|
|
}
|
|
|
|
},
|
2016-12-01 21:31:56 +00:00
|
|
|
hir::PatKind::Path(hir::QPath::Resolved(ref ty, ref path)) => {
|
|
|
|
println!("{}Resolved Path, {:?}", ind, ty);
|
2016-10-24 12:28:58 +00:00
|
|
|
println!("{}path: {:?}", ind, path);
|
|
|
|
},
|
2016-12-01 21:31:56 +00:00
|
|
|
hir::PatKind::Path(hir::QPath::TypeRelative(ref ty, ref seg)) => {
|
|
|
|
println!("{}Relative Path, {:?}", ind, ty);
|
|
|
|
println!("{}seg: {:?}", ind, seg);
|
|
|
|
},
|
2020-08-04 13:24:13 +00:00
|
|
|
hir::PatKind::Path(hir::QPath::LangItem(lang_item, ..)) => {
|
|
|
|
println!("{}Lang Item Path, {:?}", ind, lang_item.name());
|
|
|
|
},
|
2019-12-27 07:12:26 +00:00
|
|
|
hir::PatKind::Tuple(pats, opt_dots_position) => {
|
2016-10-24 12:28:58 +00:00
|
|
|
println!("{}Tuple", ind);
|
|
|
|
if let Some(dot_position) = opt_dots_position {
|
|
|
|
println!("{}dot position: {}", ind, dot_position);
|
|
|
|
}
|
|
|
|
for field in pats {
|
2016-10-24 13:16:21 +00:00
|
|
|
print_pat(cx, field, indent + 1);
|
2016-10-24 12:28:58 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
hir::PatKind::Box(ref inner) => {
|
|
|
|
println!("{}Box", ind);
|
|
|
|
print_pat(cx, inner, indent + 1);
|
|
|
|
},
|
|
|
|
hir::PatKind::Ref(ref inner, ref muta) => {
|
|
|
|
println!("{}Ref", ind);
|
|
|
|
println!("{}mutability: {:?}", ind, muta);
|
|
|
|
print_pat(cx, inner, indent + 1);
|
|
|
|
},
|
|
|
|
hir::PatKind::Lit(ref e) => {
|
|
|
|
println!("{}Lit", ind);
|
|
|
|
print_expr(cx, e, indent + 1);
|
|
|
|
},
|
2017-01-26 22:32:34 +00:00
|
|
|
hir::PatKind::Range(ref l, ref r, ref range_end) => {
|
2016-10-24 12:28:58 +00:00
|
|
|
println!("{}Range", ind);
|
2020-01-11 00:05:51 +00:00
|
|
|
if let Some(expr) = l {
|
|
|
|
print_expr(cx, expr, indent + 1);
|
|
|
|
}
|
|
|
|
if let Some(expr) = r {
|
|
|
|
print_expr(cx, expr, indent + 1);
|
|
|
|
}
|
2017-01-26 22:32:34 +00:00
|
|
|
match *range_end {
|
|
|
|
hir::RangeEnd::Included => println!("{} end included", ind),
|
|
|
|
hir::RangeEnd::Excluded => println!("{} end excluded", ind),
|
|
|
|
}
|
2016-10-24 12:28:58 +00:00
|
|
|
},
|
2019-12-27 07:12:26 +00:00
|
|
|
hir::PatKind::Slice(first_pats, ref range, last_pats) => {
|
2016-10-24 12:28:58 +00:00
|
|
|
println!("{}Slice [a, b, ..i, y, z]", ind);
|
|
|
|
println!("[a, b]:");
|
2016-10-24 13:16:21 +00:00
|
|
|
for pat in first_pats {
|
2016-10-24 12:28:58 +00:00
|
|
|
print_pat(cx, pat, indent + 1);
|
|
|
|
}
|
|
|
|
println!("i:");
|
|
|
|
if let Some(ref pat) = *range {
|
|
|
|
print_pat(cx, pat, indent + 1);
|
|
|
|
}
|
|
|
|
println!("[y, z]:");
|
2016-10-24 13:16:21 +00:00
|
|
|
for pat in last_pats {
|
2016-10-24 12:28:58 +00:00
|
|
|
print_pat(cx, pat, indent + 1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2018-09-02 07:38:25 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn print_guard(cx: &LateContext<'_>, guard: &hir::Guard<'_>, indent: usize) {
|
2018-09-02 07:38:25 +00:00
|
|
|
let ind = " ".repeat(indent);
|
|
|
|
println!("{}+", ind);
|
|
|
|
match guard {
|
|
|
|
hir::Guard::If(expr) => {
|
|
|
|
println!("{}If", ind);
|
|
|
|
print_expr(cx, expr, indent + 1);
|
2018-11-27 20:14:15 +00:00
|
|
|
},
|
2018-09-02 07:38:25 +00:00
|
|
|
}
|
|
|
|
}
|