]> git.lizzy.rs Git - rust.git/blob - src/utils.rs
Merge pull request #127 from birkenfeld/better-helptext-if-let
[rust.git] / src / utils.rs
1 use rustc::lint::{Context, Lint, Level};
2 use syntax::ast::{DefId, Expr, Name, NodeId, Path};
3 use syntax::codemap::{ExpnInfo, Span};
4 use syntax::ptr::P;
5 use rustc::ast_map::Node::NodeExpr;
6 use rustc::middle::ty;
7 use std::borrow::{Cow, IntoCow};
8 use std::convert::From;
9
10 /// returns true if the macro that expanded the crate was outside of
11 /// the current crate or was a compiler plugin
12 pub fn in_macro(cx: &Context, opt_info: Option<&ExpnInfo>) -> bool {
13     // no ExpnInfo = no macro
14     opt_info.map_or(false, |info| {
15         // no span for the callee = external macro
16         info.callee.span.map_or(true, |span| {
17             // no snippet = external macro or compiler-builtin expansion
18             cx.sess().codemap().span_to_snippet(span).ok().map_or(true, |code|
19                 // macro doesn't start with "macro_rules"
20                 // = compiler plugin
21                 !code.starts_with("macro_rules")
22             )
23         })
24     })
25 }
26
27 /// invokes in_macro with the expansion info of the given span
28 pub fn in_external_macro(cx: &Context, span: Span) -> bool {
29     cx.sess().codemap().with_expn_info(span.expn_id,
30             |info| in_macro(cx, info))
31 }
32
33 /// check if a DefId's path matches the given absolute type path
34 /// usage e.g. with
35 /// `match_def_path(cx, id, &["core", "option", "Option"])`
36 pub fn match_def_path(cx: &Context, def_id: DefId, path: &[&str]) -> bool {
37     cx.tcx.with_path(def_id, |iter| iter.map(|elem| elem.name())
38         .zip(path.iter()).all(|(nm, p)| &nm.as_str() == p))
39 }
40
41 /// match a Path against a slice of segment string literals, e.g.
42 /// `match_path(path, &["std", "rt", "begin_unwind"])`
43 pub fn match_path(path: &Path, segments: &[&str]) -> bool {
44     path.segments.iter().rev().zip(segments.iter().rev()).all(
45         |(a,b)| &a.identifier.name.as_str() == b)
46 }
47
48 /// convert a span to a code snippet if available, otherwise use default, e.g.
49 /// `snippet(cx, expr.span, "..")`
50 pub fn snippet<'a>(cx: &Context, span: Span, default: &'a str) -> Cow<'a, str> {
51     cx.sess().codemap().span_to_snippet(span).map(From::from).unwrap_or(Cow::Borrowed(default))
52 }
53
54 /// get a parent expr if any – this is useful to constrain a lint
55 pub fn get_parent_expr<'c>(cx: &'c Context, e: &Expr) -> Option<&'c Expr> {
56     let map = &cx.tcx.map;
57     let node_id : NodeId = e.id;
58     let parent_id : NodeId = map.get_parent_node(node_id);
59     if node_id == parent_id { return None; }
60     map.find(parent_id).and_then(|node|
61         if let NodeExpr(parent) = node { Some(parent) } else { None } )
62 }
63
64 /// dereference a P<T> and return a ref on the result
65 pub fn de_p<T>(p: &P<T>) -> &T { &*p }
66
67 #[cfg(not(feature="structured_logging"))]
68 pub fn span_lint(cx: &Context, lint: &'static Lint, sp: Span, msg: &str) {
69     cx.span_lint(lint, sp, msg);
70 }
71
72 #[cfg(feature="structured_logging")]
73 pub fn span_lint(cx: &Context, lint: &'static Lint, sp: Span, msg: &str) {
74     // lint.name / lint.desc is can give details of the lint
75     // cx.sess().codemap() has all these nice functions for line/column/snippet details
76     // http://doc.rust-lang.org/syntax/codemap/struct.CodeMap.html#method.span_to_string
77     cx.span_lint(lint, sp, msg);
78 }
79
80 pub fn span_help_and_lint(cx: &Context, lint: &'static Lint, span: Span,
81         msg: &str, help: &str) {
82     span_lint(cx, lint, span, msg);
83     if cx.current_level(lint) != Level::Allow {
84         cx.sess().fileline_help(span, help);
85     }
86 }