Skip to content

Various cosmetic improvements #3767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions clippy_dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ pub struct FileChange {
pub new_lines: String,
}

/// Replace a region in a file delimited by two lines matching regexes.
/// Replaces a region in a file delimited by two lines matching regexes.
///
/// `path` is the relative path to the file on which you want to perform the replacement.
///
Expand Down Expand Up @@ -225,7 +225,7 @@ where
file_change
}

/// Replace a region in a text delimited by two lines matching regexes.
/// Replaces a region in a text delimited by two lines matching regexes.
///
/// * `text` is the input text on which you want to perform the replacement
/// * `start` is a `&str` that describes the delimiter line before the region you want to replace.
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/approx_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fn check_known_consts(cx: &LateContext<'_, '_>, e: &Expr, s: symbol::Symbol, mod
}
}

/// Returns false if the number of significant figures in `value` are
/// Returns `false` if the number of significant figures in `value` are
/// less than `min_digits`; otherwise, returns true if `value` is equal
/// to `constant`, rounded to the number of digits present in `value`.
fn is_approx_const(constant: f64, value: &str, min_digits: usize) -> bool {
Expand Down
21 changes: 11 additions & 10 deletions clippy_lints/src/assertions_on_constants.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
use if_chain::if_chain;
use rustc::hir::{Expr, ExprKind};
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use rustc::{declare_tool_lint, lint_array};

use crate::consts::{constant, Constant};
use crate::rustc::hir::{Expr, ExprKind};
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use crate::rustc::{declare_tool_lint, lint_array};
use crate::syntax::ast::LitKind;
use crate::utils::{in_macro, is_direct_expn_of, span_help_and_lint};
use if_chain::if_chain;

declare_clippy_lint! {
/// **What it does:** Check to call assert!(true/false)
/// **What it does:** Checks for `assert!(true)` and `assert!(false)` calls.
///
/// **Why is this bad?** Will be optimized out by the compiler or should probably be replaced by a
/// panic!() or unreachable!()
///
/// **Known problems:** None
///
/// **Example:**
/// ```no_run
/// assert!(false);
/// ```rust,ignore
/// assert!(false)
/// // or
/// assert!(true);
/// assert!(true)
/// // or
/// const B: bool = false;
/// assert!(B);
/// assert!(B)
/// ```
pub ASSERTIONS_ON_CONSTANTS,
style,
"assert!(true/false) will be optimized out by the compiler/should probably be replaced by a panic!() or unreachable!()"
"`assert!(true)` / `assert!(false)` will be optimized out by the compiler, and should probably be replaced by a `panic!()` or `unreachable!()`"
}

pub struct AssertionsOnConstants;
Expand Down
18 changes: 10 additions & 8 deletions clippy_lints/src/assign_ops.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use crate::utils::{
get_trait_def_id, implements_trait, snippet_opt, span_lint_and_then, trait_ref_of_method, SpanlessEq,
};
use crate::utils::{higher, sugg};
use if_chain::if_chain;
use rustc::hir;
use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use rustc::{declare_tool_lint, lint_array};
use rustc_errors::Applicability;

use crate::utils::{
get_trait_def_id, implements_trait, snippet_opt, span_lint_and_then, trait_ref_of_method, SpanlessEq,
};
use crate::utils::{higher, sugg};

declare_clippy_lint! {
/// **What it does:** Checks for `a = a op b` or `a = b commutative_op a`
/// patterns.
Expand All @@ -19,9 +20,10 @@ declare_clippy_lint! {
/// implementations that differ from the regular `Op` impl.
///
/// **Example:**
/// ```ignore
/// ```rust
/// let mut a = 5;
/// ...
/// let b = 0;
/// // ...
/// a = a + b;
/// ```
pub ASSIGN_OP_PATTERN,
Expand All @@ -36,12 +38,12 @@ declare_clippy_lint! {
/// op= b`.
///
/// **Known problems:** Clippy cannot know for sure if `a op= a op b` should have
/// been `a = a op a op b` or `a = a op b`/`a op= b`. Therefore it suggests both.
/// been `a = a op a op b` or `a = a op b`/`a op= b`. Therefore, it suggests both.
/// If `a op= a op b` is really the correct behaviour it should be
/// written as `a = a op a op b` as it's less confusing.
///
/// **Example:**
/// ```ignore
/// ```rust
/// let mut a = 5;
/// ...
/// a += a + b;
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/bit_mask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ declare_clippy_lint! {
/// ```
pub INEFFECTIVE_BIT_MASK,
correctness,
"expressions where a bit mask will be rendered useless by a comparison, e.g. `(x | 1) > 2`"
"expressions where a bit mask will be rendered useless by a comparison, e.g., `(x | 1) > 2`"
}

declare_clippy_lint! {
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/block_in_if_condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ declare_clippy_lint! {
/// ```
pub BLOCK_IN_IF_CONDITION_EXPR,
style,
"braces that can be eliminated in conditions, e.g. `if { true } ...`"
"braces that can be eliminated in conditions, e.g., `if { true } ...`"
}

declare_clippy_lint! {
Expand All @@ -39,7 +39,7 @@ declare_clippy_lint! {
/// ```
pub BLOCK_IN_IF_CONDITION_STMT,
style,
"complex blocks in conditions, e.g. `if { let x = true; x } ...`"
"complex blocks in conditions, e.g., `if { let x = true; x } ...`"
}

#[derive(Copy, Clone)]
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/collapsible_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ declare_clippy_lint! {
/// ```
pub COLLAPSIBLE_IF,
style,
"`if`s that can be collapsed (e.g. `if x { if y { ... } }` and `else { if x { ... } }`)"
"`if`s that can be collapsed (e.g., `if x { if y { ... } }` and `else { if x { ... } }`)"
}

#[derive(Copy, Clone)]
Expand Down
42 changes: 22 additions & 20 deletions clippy_lints/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,27 @@ use syntax_pos::symbol::Symbol;
/// A `LitKind`-like enum to fold constant `Expr`s into.
#[derive(Debug, Clone)]
pub enum Constant {
/// a String "abc"
/// A `String` (e.g., "abc").
Str(String),
/// a Binary String b"abc"
/// A binary string (e.g., `b"abc"`).
Binary(Lrc<Vec<u8>>),
/// a single char 'a'
/// A single `char` (e.g., `'a'`).
Char(char),
/// an integer's bit representation
/// An integer's bit representation.
Int(u128),
/// an f32
/// An `f32`.
F32(f32),
/// an f64
/// An `f64`.
F64(f64),
/// true or false
/// `true` or `false`.
Bool(bool),
/// an array of constants
/// An array of constants.
Vec(Vec<Constant>),
/// also an array, but with only one constant, repeated N times
/// Also an array, but with only one constant, repeated N times.
Repeat(Box<Constant>, u64),
/// a tuple of constants
/// A tuple of constants.
Tuple(Vec<Constant>),
/// a literal with syntax error
/// A literal with syntax error.
Err(Symbol),
}

Expand All @@ -53,23 +53,24 @@ impl PartialEq for Constant {
(&Constant::Char(l), &Constant::Char(r)) => l == r,
(&Constant::Int(l), &Constant::Int(r)) => l == r,
(&Constant::F64(l), &Constant::F64(r)) => {
// we want `Fw32 == FwAny` and `FwAny == Fw64`, by transitivity we must have
// `Fw32 == Fw64` so don’t compare them
// to_bits is required to catch non-matching 0.0, -0.0, and NaNs
// We want `Fw32 == FwAny` and `FwAny == Fw64`, and by transitivity we must have
// `Fw32 == Fw64`, so don’t compare them.
// `to_bits` is required to catch non-matching 0.0, -0.0, and NaNs.
l.to_bits() == r.to_bits()
},
(&Constant::F32(l), &Constant::F32(r)) => {
// we want `Fw32 == FwAny` and `FwAny == Fw64`, by transitivity we must have
// `Fw32 == Fw64` so don’t compare them
// to_bits is required to catch non-matching 0.0, -0.0, and NaNs
// We want `Fw32 == FwAny` and `FwAny == Fw64`, and by transitivity we must have
// `Fw32 == Fw64`, so don’t compare them.
// `to_bits` is required to catch non-matching 0.0, -0.0, and NaNs.
f64::from(l).to_bits() == f64::from(r).to_bits()
},
(&Constant::Bool(l), &Constant::Bool(r)) => l == r,
(&Constant::Vec(ref l), &Constant::Vec(ref r)) | (&Constant::Tuple(ref l), &Constant::Tuple(ref r)) => {
l == r
},
(&Constant::Repeat(ref lv, ref ls), &Constant::Repeat(ref rv, ref rs)) => ls == rs && lv == rv,
_ => false, // TODO: Are there inter-type equalities?
// TODO: are there inter-type equalities?
_ => false,
}
}
}
Expand Down Expand Up @@ -142,12 +143,13 @@ impl Constant {
x => x,
}
},
_ => None, // TODO: Are there any useful inter-type orderings?
// TODO: are there any useful inter-type orderings?
_ => None,
}
}
}

/// parse a `LitKind` to a `Constant`
/// Parses a `LitKind` to a `Constant`.
pub fn lit_to_constant<'tcx>(lit: &LitKind, ty: Ty<'tcx>) -> Constant {
use syntax::ast::*;

Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/copies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,9 @@ fn lint_match_arms(cx: &LateContext<'_, '_>, expr: &Expr) {
}
}

/// Return the list of condition expressions and the list of blocks in a
/// Returns the list of condition expressions and the list of blocks in a
/// sequence of `if/else`.
/// Eg. would return `([a, b], [c, d, e])` for the expression
/// E.g., this returns `([a, b], [c, d, e])` for the expression
/// `if a { c } else if b { d } else { e }`.
fn if_sequence(mut expr: &Expr) -> (SmallVec<[&Expr; 1]>, SmallVec<[&Block; 1]>) {
let mut conds = SmallVec::new();
Expand Down Expand Up @@ -272,7 +272,7 @@ fn if_sequence(mut expr: &Expr) -> (SmallVec<[&Expr; 1]>, SmallVec<[&Block; 1]>)
(conds, blocks)
}

/// Return the list of bindings in a pattern.
/// Returns the list of bindings in a pattern.
fn bindings<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, pat: &Pat) -> FxHashMap<LocalInternedString, Ty<'tcx>> {
fn bindings_impl<'a, 'tcx>(
cx: &LateContext<'a, 'tcx>,
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/deprecated_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ declare_deprecated_lint! {
/// counterparts, so this lint may suggest a change in behavior or the code may not compile.
declare_deprecated_lint! {
pub ASSIGN_OPS,
"using compound assignment operators (e.g. `+=`) is harmless"
"using compound assignment operators (e.g., `+=`) is harmless"
}

/// **What it does:** Nothing. This lint has been deprecated.
Expand Down
7 changes: 3 additions & 4 deletions clippy_lints/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,9 @@ fn check_text(cx: &EarlyContext<'_>, valid_idents: &FxHashSet<String>, text: &st
}

fn check_word(cx: &EarlyContext<'_>, word: &str, span: Span) {
/// Checks if a string is camel-case, ie. contains at least two uppercase
/// letter (`Clippy` is
/// ok) and one lower-case letter (`NASA` is ok). Plural are also excluded
/// (`IDs` is ok).
/// Checks if a string is camel-case, i.e., contains at least two uppercase
/// letters (`Clippy` is ok) and one lower-case letter (`NASA` is ok).
/// Plurals are also excluded (`IDs` is ok).
fn is_camel_case(s: &str) -> bool {
if s.starts_with(|c: char| c.is_digit(10)) {
return false;
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/else_if_without_else.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! lint on if expressions with an else if, but without a final else branch
//! Lint on if expressions with an else if, but without a final else branch.

use rustc::lint::{in_external_macro, EarlyContext, EarlyLintPass, LintArray, LintContext, LintPass};
use rustc::{declare_tool_lint, lint_array};
Expand All @@ -10,7 +10,7 @@ declare_clippy_lint! {
/// **What it does:** Checks for usage of if expressions with an `else if` branch,
/// but without a final `else` branch.
///
/// **Why is this bad?** Some coding guidelines require this (e.g. MISRA-C:2004 Rule 14.10).
/// **Why is this bad?** Some coding guidelines require this (e.g., MISRA-C:2004 Rule 14.10).
///
/// **Known problems:** None.
///
Expand All @@ -31,7 +31,7 @@ declare_clippy_lint! {
/// } else if x.is_negative() {
/// b();
/// } else {
/// // we don't care about zero
/// // We don't care about zero.
/// }
/// ```
pub ELSE_IF_WITHOUT_ELSE,
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/eq_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ declare_clippy_lint! {
/// ```
pub EQ_OP,
correctness,
"equal operands on both sides of a comparison or bitwise combination (e.g. `x == x`)"
"equal operands on both sides of a comparison or bitwise combination (e.g., `x == x`)"
}

declare_clippy_lint! {
Expand Down
14 changes: 8 additions & 6 deletions clippy_lints/src/erasing_op.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::consts::{constant_simple, Constant};
use crate::utils::{in_macro, span_lint};
use rustc::hir::*;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use rustc::{declare_tool_lint, lint_array};
use syntax::source_map::Span;

use crate::consts::{constant_simple, Constant};
use crate::utils::{in_macro, span_lint};

declare_clippy_lint! {
/// **What it does:** Checks for erasing operations, e.g. `x * 0`.
/// **What it does:** Checks for erasing operations, e.g., `x * 0`.
///
/// **Why is this bad?** The whole expression can be replaced by zero.
/// This is most likely not the intended outcome and should probably be
Expand All @@ -15,14 +16,15 @@ declare_clippy_lint! {
/// **Known problems:** None.
///
/// **Example:**
/// ```ignore
/// ```rust
/// let x = 1;
/// 0 / x;
/// 0 * x;
/// x & 0
/// x & 0;
/// ```
pub ERASING_OP,
correctness,
"using erasing operations, e.g. `x * 0` or `y & 0`"
"using erasing operations, e.g., `x * 0` or `y & 0`"
}

#[derive(Copy, Clone)]
Expand Down
Loading