Auto merge of #4325 - phansch:doctests_complexity, r=flip1995

Doctests: Enable running doc tests for complexity lints

changelog: none

master: `113 passed; 0 failed; 91 ignored; 0 measured; 0 filtered out`
this PR: `181 passed; 0 failed; 110 ignored; 0 measured; 0 filtered out`

cc #4319
This commit is contained in:
bors 2019-08-03 13:24:07 +00:00
commit 3dc91838e4
33 changed files with 157 additions and 85 deletions

View File

@ -45,7 +45,8 @@ declare_clippy_lint! {
/// **Example:**
/// ```rust
/// let mut a = 5;
/// ...
/// let b = 2;
/// // ...
/// a += a + b;
/// ```
pub MISREFACTORED_ASSIGN_OP,

View File

@ -18,13 +18,17 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// x == y || x < y
/// # let x = 1;
/// # let y = 2;
/// if x == y || x < y {}
/// ```
///
/// Could be written as:
///
/// ```rust
/// x <= y
/// # let x = 1;
/// # let y = 2;
/// if x <= y {}
/// ```
pub DOUBLE_COMPARISONS,
complexity,

View File

@ -13,9 +13,10 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// ((0))
/// foo((0))
/// ((1, 2))
/// # fn foo(bar: usize) {}
/// ((0));
/// foo((0));
/// ((1, 2));
/// ```
pub DOUBLE_PARENS,
complexity,

View File

@ -20,6 +20,7 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// # use std::time::Duration;
/// let dur = Duration::new(5, 0);
/// let _micros = dur.subsec_nanos() / 1_000;
/// let _millis = dur.subsec_nanos() / 1_000_000;

View File

@ -42,7 +42,9 @@ declare_clippy_lint! {
/// shorthand.
///
/// **Example:**
/// ```rust
/// ```rust,no_run
/// # fn b() -> bool { true }
/// # fn c() -> bool { true }
/// let a = b() || panic!() || c();
/// // `c()` is dead, `panic!()` is only called if `b()` returns `false`
/// let x = (a, b, c, panic!());

View File

@ -16,8 +16,10 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// # use std::io::Write;
/// # let bar = "furchtbar";
/// // this would be clearer as `eprintln!("foo: {:?}", bar);`
/// writeln!(&mut io::stderr(), "foo: {:?}", bar).unwrap();
/// writeln!(&mut std::io::stderr(), "foo: {:?}", bar).unwrap();
/// ```
pub EXPLICIT_WRITE,
complexity,

View File

@ -26,8 +26,9 @@ declare_clippy_lint! {
///
/// **Examples:**
/// ```rust
/// format!("foo")
/// format!("{}", foo)
/// # let foo = "foo";
/// format!("foo");
/// format!("{}", foo);
/// ```
pub USELESS_FORMAT,
complexity,

View File

@ -23,8 +23,9 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// # struct Color;
/// fn foo(x: u32, y: u32, name: &str, c: Color, w: f32, h: f32, a: f32, b: f32) {
/// ..
/// // ..
/// }
/// ```
pub TOO_MANY_ARGUMENTS,

View File

@ -17,7 +17,8 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// x / 1 + 0 * 1 - 0 | 0
/// # let x = 1;
/// x / 1 + 0 * 1 - 0 | 0;
/// ```
pub IDENTITY_OP,
complexity,

View File

@ -17,13 +17,17 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// x >= y + 1
/// # let x = 1;
/// # let y = 1;
/// if x >= y + 1 {}
/// ```
///
/// Could be written:
/// Could be written as:
///
/// ```rust
/// x > y
/// # let x = 1;
/// # let y = 1;
/// if x > y {}
/// ```
pub INT_PLUS_ONE,
complexity,

View File

@ -100,7 +100,7 @@ macro_rules! declare_clippy_lint {
};
{ $(#[$attr:meta])* pub $name:tt, complexity, $description:tt } => {
declare_tool_lint! {
pub clippy::$name, Warn, $description, report_in_external_macro: true
$(#[$attr])* pub clippy::$name, Warn, $description, report_in_external_macro: true
}
};
{ $(#[$attr:meta])* pub $name:tt, perf, $description:tt } => {
@ -130,12 +130,12 @@ macro_rules! declare_clippy_lint {
};
{ $(#[$attr:meta])* pub $name:tt, internal, $description:tt } => {
declare_tool_lint! {
pub clippy::$name, Allow, $description, report_in_external_macro: true
$(#[$attr])* pub clippy::$name, Allow, $description, report_in_external_macro: true
}
};
{ $(#[$attr:meta])* pub $name:tt, internal_warn, $description:tt } => {
declare_tool_lint! {
pub clippy::$name, Warn, $description, report_in_external_macro: true
$(#[$attr])* pub clippy::$name, Warn, $description, report_in_external_macro: true
}
};
}

View File

@ -47,7 +47,7 @@ declare_clippy_lint! {
/// **Example:**
/// ```rust
/// fn unused_lifetime<'a>(x: u8) {
/// ..
/// // ..
/// }
/// ```
pub EXTRA_UNUSED_LIFETIMES,

View File

@ -217,18 +217,19 @@ declare_clippy_lint! {
/// **Known problems:** Sometimes the wrong binding is displayed (#383).
///
/// **Example:**
/// ```rust
/// ```rust,no_run
/// # let y = Some(1);
/// loop {
/// let x = match y {
/// Some(x) => x,
/// None => break,
/// }
/// };
/// // .. do something with x
/// }
/// // is easier written as
/// while let Some(x) = y {
/// // .. do something with x
/// }
/// };
/// ```
pub WHILE_LET_LOOP,
complexity,
@ -309,8 +310,11 @@ declare_clippy_lint! {
/// **Known problems:** None.
///
/// **Example:**
/// ```ignore
/// for i in 0..v.len() { foo(v[i]);
/// ```rust
/// # let v = vec![1];
/// # fn foo(bar: usize) {}
/// # fn bar(bar: usize, baz: usize) {}
/// for i in 0..v.len() { foo(v[i]); }
/// for i in 0..v.len() { bar(i, v[i]); }
/// ```
pub EXPLICIT_COUNTER_LOOP,

View File

@ -20,20 +20,29 @@ declare_clippy_lint! {
/// **Example:**
///
/// ```rust
/// let x: Option<&str> = do_stuff();
/// # fn do_stuff() -> Option<String> { Some(String::new()) }
/// # fn log_err_msg(foo: String) -> Option<String> { Some(foo) }
/// # fn format_msg(foo: String) -> String { String::new() }
/// let x: Option<String> = do_stuff();
/// x.map(log_err_msg);
/// x.map(|msg| log_err_msg(format_msg(msg)))
/// # let x: Option<String> = do_stuff();
/// x.map(|msg| log_err_msg(format_msg(msg)));
/// ```
///
/// The correct use would be:
///
/// ```rust
/// let x: Option<&str> = do_stuff();
/// # fn do_stuff() -> Option<String> { Some(String::new()) }
/// # fn log_err_msg(foo: String) -> Option<String> { Some(foo) }
/// # fn format_msg(foo: String) -> String { String::new() }
/// let x: Option<String> = do_stuff();
/// if let Some(msg) = x {
/// log_err_msg(msg)
/// log_err_msg(msg);
/// }
///
/// # let x: Option<String> = do_stuff();
/// if let Some(msg) = x {
/// log_err_msg(format_msg(msg))
/// log_err_msg(format_msg(msg));
/// }
/// ```
pub OPTION_MAP_UNIT_FN,
@ -53,21 +62,29 @@ declare_clippy_lint! {
/// **Example:**
///
/// ```rust
/// let x: Result<&str, &str> = do_stuff();
/// # fn do_stuff() -> Result<String, String> { Ok(String::new()) }
/// # fn log_err_msg(foo: String) -> Result<String, String> { Ok(foo) }
/// # fn format_msg(foo: String) -> String { String::new() }
/// let x: Result<String, String> = do_stuff();
/// x.map(log_err_msg);
/// x.map(|msg| log_err_msg(format_msg(msg)))
/// # let x: Result<String, String> = do_stuff();
/// x.map(|msg| log_err_msg(format_msg(msg)));
/// ```
///
/// The correct use would be:
///
/// ```rust
/// let x: Result<&str, &str> = do_stuff();
/// # fn do_stuff() -> Result<String, String> { Ok(String::new()) }
/// # fn log_err_msg(foo: String) -> Result<String, String> { Ok(foo) }
/// # fn format_msg(foo: String) -> String { String::new() }
/// let x: Result<String, String> = do_stuff();
/// if let Ok(msg) = x {
/// log_err_msg(msg)
/// }
/// log_err_msg(msg);
/// };
/// # let x: Result<String, String> = do_stuff();
/// if let Ok(msg) = x {
/// log_err_msg(format_msg(msg))
/// }
/// log_err_msg(format_msg(msg));
/// };
/// ```
pub RESULT_MAP_UNIT_FN,
complexity,

View File

@ -247,7 +247,8 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// iter.filter(|x| x == 0).next()
/// # let vec = vec![1];
/// vec.iter().filter(|x| **x == 0).next();
/// ```
pub FILTER_NEXT,
complexity,
@ -345,7 +346,8 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// iter.find(|x| x == 0).is_some()
/// # let vec = vec![1];
/// vec.iter().find(|x| **x == 0).is_some();
/// ```
pub SEARCH_IS_SOME,
complexity,
@ -363,7 +365,8 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// name.chars().next() == Some('_')
/// let name = "foo";
/// name.chars().next() == Some('_');
/// ```
pub CHARS_NEXT_CMP,
complexity,
@ -434,7 +437,7 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// 42u64.clone()
/// 42u64.clone();
/// ```
pub CLONE_ON_COPY,
complexity,
@ -708,11 +711,13 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// # fn do_stuff(x: &[i32]) {}
/// let x: &[i32] = &[1, 2, 3, 4, 5];
/// do_stuff(x.as_ref());
/// ```
/// The correct use would be:
/// ```rust
/// # fn do_stuff(x: &[i32]) {}
/// let x: &[i32] = &[1, 2, 3, 4, 5];
/// do_stuff(x);
/// ```

View File

@ -184,7 +184,7 @@ declare_clippy_lint! {
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// ```rust,ignore
/// f() && g(); // We should write `if f() { g(); }`.
/// ```
pub SHORT_CIRCUIT_STATEMENT,

View File

@ -53,7 +53,7 @@ declare_clippy_lint! {
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// ```rust,ignore
/// (|| 42)()
/// ```
pub REDUNDANT_CLOSURE_CALL,

View File

@ -24,7 +24,7 @@ declare_clippy_lint! {
/// shorter code.
///
/// **Example:**
/// ```rust
/// ```rust,ignore
/// if x {
/// false
/// } else {
@ -46,7 +46,7 @@ declare_clippy_lint! {
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// ```rust,ignore
/// if x == true {} // could be `if x { }`
/// ```
pub BOOL_COMPARISON,

View File

@ -18,7 +18,7 @@ declare_clippy_lint! {
///
/// **Known problems:** It seems that the `&ref` pattern is sometimes useful.
/// For instance in the following snippet:
/// ```rust
/// ```rust,ignore
/// enum Animal {
/// Cat(u64),
/// Dog(u64),
@ -26,8 +26,7 @@ declare_clippy_lint! {
///
/// fn foo(a: &Animal, b: &Animal) {
/// match (a, b) {
/// (&Animal::Cat(v), k) | (k, &Animal::Cat(v)) => (), // lifetime
/// mismatch error
/// (&Animal::Cat(v), k) | (k, &Animal::Cat(v)) => (), // lifetime mismatch error
/// (&Animal::Dog(ref c), &Animal::Dog(_)) => ()
/// }
/// }

View File

@ -15,11 +15,17 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// # struct Point {
/// # x: i32,
/// # y: i32,
/// # z: i32,
/// # }
/// # let zero_point = Point { x: 0, y: 0, z: 0 };
/// Point {
/// x: 1,
/// y: 0,
/// y: 1,
/// ..zero_point
/// }
/// };
/// ```
pub NEEDLESS_UPDATE,
complexity,

View File

@ -34,7 +34,7 @@ declare_clippy_lint! {
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// ```rust,ignore
/// compute_array()[0];
/// ```
pub UNNECESSARY_OPERATION,

View File

@ -14,7 +14,9 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// a + b < a
/// # let a = 1;
/// # let b = 2;
/// a + b < a;
/// ```
pub OVERFLOW_CHECK_CONDITIONAL,
complexity,

View File

@ -19,7 +19,7 @@ declare_clippy_lint! {
/// struct Foo;
///
/// impl PartialEq for Foo {
/// fn eq(&self, other: &Foo) -> bool { ... }
/// fn eq(&self, other: &Foo) -> bool { true }
/// fn ne(&self, other: &Foo) -> bool { !(self == other) }
/// }
/// ```

View File

@ -40,7 +40,8 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// x.iter().zip(0..x.len())
/// # let x = vec![1];
/// x.iter().zip(0..x.len());
/// ```
pub RANGE_ZIP_WITH_LEN,
complexity,
@ -60,7 +61,7 @@ declare_clippy_lint! {
/// I.e., `let _ = (f()+1)..(f()+1)` results in `let _ = ((f()+1)..=f())`.
///
/// **Example:**
/// ```rust
/// ```rust,ignore
/// for x..(y+1) { .. }
/// ```
pub RANGE_PLUS_ONE,
@ -78,7 +79,7 @@ declare_clippy_lint! {
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// ```rust,ignore
/// for x..=(y-1) { .. }
/// ```
pub RANGE_MINUS_ONE,

View File

@ -15,7 +15,7 @@ declare_clippy_lint! {
/// the suggested fix for `x = **&&y` is `x = *&y`, which is still incorrect.
///
/// **Example:**
/// ```rust
/// ```rust,ignore
/// let a = f(*&mut b);
/// let c = *&d;
/// ```
@ -64,8 +64,8 @@ declare_clippy_lint! {
/// **Example:**
/// ```rust
/// struct Point(u32, u32);
/// let point = Foo(30, 20);
/// let x = (&point).x;
/// let point = Point(30, 20);
/// let x = (&point).0;
/// ```
pub REF_IN_DEREF,
complexity,

View File

@ -20,12 +20,17 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// let mut a = 42;
/// let mut b = 1337;
///
/// let t = b;
/// b = a;
/// a = t;
/// ```
/// Use std::mem::swap():
/// ```rust
/// let mut a = 1;
/// let mut b = 2;
/// std::mem::swap(&mut a, &mut b);
/// ```
pub MANUAL_SWAP,

View File

@ -15,13 +15,13 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// pub fn foo<T>(t: T) where T: Copy, T: Clone
/// pub fn foo<T>(t: T) where T: Copy, T: Clone {}
/// ```
///
/// Could be written as:
///
/// ```rust
/// pub fn foo<T>(t: T) where T: Copy + Clone
/// pub fn foo<T>(t: T) where T: Copy + Clone {}
/// ```
pub TYPE_REPETITION_IN_BOUNDS,
complexity,

View File

@ -36,8 +36,8 @@ declare_clippy_lint! {
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// core::intrinsics::transmute(t) // where the result type is the same as `t`'s
/// ```rust,ignore
/// core::intrinsics::transmute(t); // where the result type is the same as `t`'s
/// ```
pub USELESS_TRANSMUTE,
complexity,
@ -53,7 +53,7 @@ declare_clippy_lint! {
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// ```rust,ignore
/// core::intrinsics::transmute(t) // where the result type is the same as
/// // `*t` or `&t`'s
/// ```
@ -70,8 +70,10 @@ declare_clippy_lint! {
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// let _: &T = std::mem::transmute(p); // where p: *const T
/// ```rust,ignore
/// unsafe {
/// let _: &T = std::mem::transmute(p); // where p: *const T
/// }
///
/// // can be written:
/// let _: &T = &*p;
@ -99,7 +101,10 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// let _: char = std::mem::transmute(x); // where x: u32
/// let x = 1_u32;
/// unsafe {
/// let _: char = std::mem::transmute(x); // where x: u32
/// }
///
/// // should be:
/// let _ = std::char::from_u32(x).unwrap();
@ -127,7 +132,10 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// let _: &str = std::mem::transmute(b); // where b: &[u8]
/// let b: &[u8] = &[1_u8, 2_u8];
/// unsafe {
/// let _: &str = std::mem::transmute(b); // where b: &[u8]
/// }
///
/// // should be:
/// let _ = std::str::from_utf8(b).unwrap();
@ -146,7 +154,10 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// let _: bool = std::mem::transmute(x); // where x: u8
/// let x = 1_u8;
/// unsafe {
/// let _: bool = std::mem::transmute(x); // where x: u8
/// }
///
/// // should be:
/// let _: bool = x != 0;
@ -166,10 +177,12 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// let _: f32 = std::mem::transmute(x); // where x: u32
/// unsafe {
/// let _: f32 = std::mem::transmute(1_u32); // where x: u32
/// }
///
/// // should be:
/// let _: f32 = f32::from_bits(x);
/// let _: f32 = f32::from_bits(1_u32);
/// ```
pub TRANSMUTE_INT_TO_FLOAT,
complexity,
@ -195,7 +208,7 @@ declare_clippy_lint! {
/// let _: &f32 = std::mem::transmute(&1u32);
/// }
/// // These can be respectively written:
/// let _ = ptr as *const f32
/// let _ = ptr as *const f32;
/// let _ = unsafe{ &*(&1u32 as *const u32 as *const f32) };
/// ```
pub TRANSMUTE_PTR_TO_PTR,

View File

@ -557,7 +557,7 @@ declare_clippy_lint! {
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// ```rust,ignore
/// foo({
/// let a = bar();
/// baz(a);
@ -775,7 +775,7 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// let _ = 2i32 as i32
/// let _ = 2i32 as i32;
/// ```
pub UNNECESSARY_CAST,
complexity,
@ -1295,6 +1295,7 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// # use std::rc::Rc;
/// struct Foo {
/// inner: Rc<Vec<Vec<Box<(u32, u32, u32, u32)>>>>,
/// }
@ -1458,13 +1459,13 @@ declare_clippy_lint! {
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// ```rust,ignore
/// 'x' as u8
/// ```
///
/// A better version, using the byte literal:
///
/// ```rust
/// ```rust,ignore
/// b'x'
/// ```
pub CHAR_LIT_AS_U8,

View File

@ -15,7 +15,7 @@ declare_clippy_lint! {
/// **What it does:** Generates clippy code that detects the offending pattern
///
/// **Example:**
/// ```rust
/// ```rust,ignore
/// // ./tests/ui/my_lint.rs
/// fn foo() {
/// // detect the following pattern
@ -24,13 +24,14 @@ declare_clippy_lint! {
/// // but ignore everything from here on
/// #![clippy::author = "ignore"]
/// }
/// ()
/// }
/// ```
///
/// Running `TESTNAME=ui/my_lint cargo uitest` will produce
/// a `./tests/ui/new_lint.stdout` file with the generated code:
///
/// ```rust
/// ```rust,ignore
/// // ./tests/ui/new_lint.stdout
/// if_chain! {
/// if let ExprKind::If(ref cond, ref then, None) = item.node,

View File

@ -13,14 +13,14 @@ declare_clippy_lint! {
/// attribute
///
/// **Example:**
/// ```rust
/// ```rust,ignore
/// #[clippy::dump]
/// extern crate foo;
/// ```
///
/// prints
///
/// ```
/// ```text
/// item `foo`
/// visibility inherited from outer item
/// extern crate dylib source: "/path/to/foo.so"

View File

@ -38,7 +38,7 @@ declare_clippy_lint! {
/// `declare_lint_pass!`, `impl_lint_pass!`, and `lint_array!` macros.
///
/// **Example:**
/// ```rust
/// ```rust,ignore
/// declare_lint! { pub LINT_1, ... }
/// declare_lint! { pub LINT_2, ... }
/// declare_lint! { pub FORGOTTEN_LINT, ... }
@ -62,12 +62,12 @@ declare_clippy_lint! {
///
/// **Example:**
/// Bad:
/// ```rust
/// ```rust,ignore
/// cx.span_lint(LINT_NAME, "message");
/// ```
///
/// Good:
/// ```rust
/// ```rust,ignore
/// utils::span_lint(cx, LINT_NAME, "message");
/// ```
pub COMPILER_LINT_FUNCTIONS,
@ -85,12 +85,12 @@ declare_clippy_lint! {
///
/// **Example:**
/// Bad:
/// ```rust
/// ```rust,ignore
/// expr.span.ctxt().outer().expn_info()
/// ```
///
/// Good:
/// ```rust
/// ```rust,ignore
/// expr.span.ctxt().outer_expn_info()
/// ```
pub OUTER_EXPN_EXPN_INFO,

View File

@ -15,7 +15,7 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// 0.0f32 / 0.0
/// 0.0f32 / 0.0;
/// ```
pub ZERO_DIVIDED_BY_ZERO,
complexity,