/// the corresponding trait instead.
///
/// **Why is this bad?**: Calling '.clone()' on an Rc, Arc, or Weak
-/// can obscure the fact that only the pointer is being cloned, not the
-/// underlying
+/// can obscure the fact that only the pointer is being cloned, not the underlying
/// data.
///
/// **Example:**
expr.span,
"using '.clone()' on a ref-counted pointer",
"try this",
- format!("{}::clone(&{})", caller_type, snippet(cx, arg.span, "_")),
+ format!("{}::clone(&{})",
+ caller_type,
+ snippet(cx, arg.span, "_")
+ )
);
+
}
}
fn lint_iter_cloned_collect(cx: &LateContext, expr: &hir::Expr, iter_args: &[hir::Expr]) {
- if match_type(cx, cx.tables.expr_ty(expr), &paths::VEC)
- && derefs_to_slice(cx, &iter_args[0], cx.tables.expr_ty(&iter_args[0])).is_some()
+ if match_type(cx, cx.tables.expr_ty(expr), &paths::VEC) &&
+ derefs_to_slice(cx, &iter_args[0], cx.tables.expr_ty(&iter_args[0])).is_some()
{
span_lint(
cx,
// lint message
// comparing the snippet from source to raw text ("None") below is safe
// because we already have checked the type.
- let arg = if unwrap_snippet == "None" {
- "None"
- } else {
- "a"
- };
- let suggest = if unwrap_snippet == "None" {
- "and_then(f)"
- } else {
- "map_or(a, f)"
- };
+ let arg = if unwrap_snippet == "None" { "None" } else { "a" };
+ let suggest = if unwrap_snippet == "None" { "and_then(f)" } else { "map_or(a, f)" };
let msg = &format!(
"called `map(f).unwrap_or({})` on an Option value. \
This can be done more directly by calling `{}` instead",
}
/// lint use of `map().unwrap_or_else()` for `Option`s
-fn lint_map_unwrap_or_else<'a, 'tcx>(
- cx: &LateContext<'a, 'tcx>,
- expr: &'tcx hir::Expr,
- map_args: &'tcx [hir::Expr],
- unwrap_args: &'tcx [hir::Expr],
-) {
+fn lint_map_unwrap_or_else<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr, map_args: &'tcx [hir::Expr], unwrap_args: &'tcx [hir::Expr]) {
// lint if the caller of `map()` is an `Option`
if match_type(cx, cx.tables.expr_ty(&map_args[0]), &paths::OPTION) {
// lint message
/// lint use of `_.map_or(None, _)` for `Option`s
fn lint_map_or_none<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr, map_or_args: &'tcx [hir::Expr]) {
+
if match_type(cx, cx.tables.expr_ty(&map_or_args[0]), &paths::OPTION) {
// check if the first non-self argument to map_or() is None
let map_or_arg_is_none = if let hir::Expr_::ExprPath(ref qpath) = map_or_args[1].node {
let map_or_self_snippet = snippet(cx, map_or_args[0].span, "..");
let map_or_func_snippet = snippet(cx, map_or_args[2].span, "..");
let hint = format!("{0}.and_then({1})", map_or_self_snippet, map_or_func_snippet);
- span_lint_and_then(cx, OPTION_MAP_OR_NONE, expr.span, msg, |db| {
- db.span_suggestion(expr.span, "try using and_then instead", hint);
- });
+ span_lint_and_then(
+ cx,
+ OPTION_MAP_OR_NONE,
+ expr.span,
+ msg,
+ |db| { db.span_suggestion(expr.span, "try using and_then instead", hint); },
+ );
}
}
}
}
/// lint use of `filter().map()` for `Iterators`
-fn lint_filter_map<'a, 'tcx>(
- cx: &LateContext<'a, 'tcx>,
- expr: &'tcx hir::Expr,
- _filter_args: &'tcx [hir::Expr],
- _map_args: &'tcx [hir::Expr],
-) {
+fn lint_filter_map<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr, _filter_args: &'tcx [hir::Expr], _map_args: &'tcx [hir::Expr]) {
// lint if caller of `.filter().map()` is an Iterator
if match_trait_method(cx, expr, &paths::ITERATOR) {
let msg = "called `filter(p).map(q)` on an `Iterator`. \
}
/// lint use of `filter().map()` for `Iterators`
-fn lint_filter_map_map<'a, 'tcx>(
- cx: &LateContext<'a, 'tcx>,
- expr: &'tcx hir::Expr,
- _filter_args: &'tcx [hir::Expr],
- _map_args: &'tcx [hir::Expr],
-) {
+fn lint_filter_map_map<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr, _filter_args: &'tcx [hir::Expr], _map_args: &'tcx [hir::Expr]) {
// lint if caller of `.filter().map()` is an Iterator
if match_trait_method(cx, expr, &paths::ITERATOR) {
let msg = "called `filter_map(p).map(q)` on an `Iterator`. \
}
/// lint use of `filter().flat_map()` for `Iterators`
-fn lint_filter_flat_map<'a, 'tcx>(
- cx: &LateContext<'a, 'tcx>,
- expr: &'tcx hir::Expr,
- _filter_args: &'tcx [hir::Expr],
- _map_args: &'tcx [hir::Expr],
-) {
+fn lint_filter_flat_map<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr, _filter_args: &'tcx [hir::Expr], _map_args: &'tcx [hir::Expr]) {
// lint if caller of `.filter().flat_map()` is an Iterator
if match_trait_method(cx, expr, &paths::ITERATOR) {
let msg = "called `filter(p).flat_map(q)` on an `Iterator`. \
}
/// lint use of `filter_map().flat_map()` for `Iterators`
-fn lint_filter_map_flat_map<'a, 'tcx>(
- cx: &LateContext<'a, 'tcx>,
- expr: &'tcx hir::Expr,
- _filter_args: &'tcx [hir::Expr],
- _map_args: &'tcx [hir::Expr],
-) {
+fn lint_filter_map_flat_map<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr, _filter_args: &'tcx [hir::Expr], _map_args: &'tcx [hir::Expr]) {
// lint if caller of `.filter_map().flat_map()` is an Iterator
if match_trait_method(cx, expr, &paths::ITERATOR) {
let msg = "called `filter_map(p).flat_map(q)` on an `Iterator`. \
}
/// Wrapper fn for `CHARS_NEXT_CMP` and `CHARS_NEXT_CMP` lints.
-fn lint_chars_cmp<'a, 'tcx>(
- cx: &LateContext<'a, 'tcx>,
- info: &BinaryExprInfo,
- chain_methods: &[&str],
- lint: &'static Lint,
- suggest: &str,
-) -> bool {
+fn lint_chars_cmp<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, info: &BinaryExprInfo, chain_methods: &[&str], lint: &'static Lint, suggest: &str) -> bool {
if_let_chain! {[
let Some(args) = method_chain_args(info.chain, chain_methods),
let hir::ExprCall(ref fun, ref arg_char) = info.other.node,
}
/// Wrapper fn for `CHARS_NEXT_CMP` and `CHARS_LAST_CMP` lints with `unwrap()`.
-fn lint_chars_cmp_with_unwrap<'a, 'tcx>(
- cx: &LateContext<'a, 'tcx>,
- info: &BinaryExprInfo,
- chain_methods: &[&str],
- lint: &'static Lint,
- suggest: &str,
-) -> bool {
+fn lint_chars_cmp_with_unwrap<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, info: &BinaryExprInfo, chain_methods: &[&str], lint: &'static Lint, suggest: &str) -> bool {
if_let_chain! {[
let Some(args) = method_chain_args(info.chain, chain_methods),
let hir::ExprLit(ref lit) = info.other.node,
let parent_item = cx.tcx.hir.get_parent(arg.id);
let parent_def_id = cx.tcx.hir.local_def_id(parent_item);
let substs = Substs::identity_for_item(cx.tcx, parent_def_id);
- if let Ok(&ty::Const {
- val: ConstVal::Str(r),
- ..
- }) = ConstContext::new(cx.tcx, cx.param_env.and(substs), cx.tables).eval(arg)
- {
+ if let Ok(&ty::Const { val: ConstVal::Str(r), .. }) = ConstContext::new(cx.tcx, cx.param_env.and(substs), cx.tables).eval(arg) {
if r.len() == 1 {
let hint = snippet(cx, expr.span, "..").replace(&format!("\"{}\"", r), &format!("'{}'", r));
span_lint_and_then(
SINGLE_CHAR_PATTERN,
arg.span,
"single-character string constant used as pattern",
- |db| {
- db.span_suggestion(expr.span, "try using a char instead", hint);
- },
+ |db| { db.span_suggestion(expr.span, "try using a char instead", hint); },
);
}
}
fn is_as_ref_or_mut_trait(ty: &hir::Ty, self_ty: &hir::Ty, generics: &hir::Generics, name: &[&str]) -> bool {
single_segment_ty(ty).map_or(false, |seg| {
generics.ty_params.iter().any(|param| {
- param.name == seg.name
- && param
+ param.name == seg.name &&
+ param
.bounds
.iter()
.any(|bound| if let hir::TyParamBound::TraitTyParamBound(ref ptr, ..) = *bound {
let path = &ptr.trait_ref.path;
- match_path(path, name)
- && path.segments
+ match_path(path, name) &&
+ path.segments
.last()
- .map_or(false, |s| if let Some(ref params) = s.parameters {
- if params.parenthesized {
- false
+ .map_or(false, |s| {
+ if let Some(ref params) = s.parameters {
+ if params.parenthesized {
+ false
+ } else {
+ params.types.len() == 1 &&
+ (is_self_ty(¶ms.types[0])
+ || is_ty(&*params.types[0], self_ty))
+ }
} else {
- params.types.len() == 1
- && (is_self_ty(¶ms.types[0]) || is_ty(&*params.types[0], self_ty))
+ false
}
- } else {
- false
})
} else {
false