Reworked the error messages for more heplfulness.
Renamed the cast_possible_overflow lint to cast_possible_truncation, and updated the error message, readme and crate root accordingly. Added some more information to the message for the cast_precision_loss lint. Updated the test case to reflect changes.
This commit is contained in:
parent
ab481e5cb1
commit
dbc9b7f46e
88
README.md
88
README.md
@ -6,50 +6,50 @@ A collection of lints that give helpful tips to newbies and catch oversights.
|
||||
##Lints
|
||||
Lints included in this crate:
|
||||
|
||||
name | default | meaning
|
||||
-----------------------|---------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
approx_constant | warn | the approximate of a known float constant (in `std::f64::consts` or `std::f32::consts`) is found; suggests to use the constant
|
||||
bad_bit_mask | deny | expressions of the form `_ & mask == select` that will only ever return `true` or `false` (because in the example `select` containing bits that `mask` doesn't have)
|
||||
box_vec | warn | usage of `Box<Vec<T>>`, vector elements are already on the heap
|
||||
cast_possible_overflow | allow | casts that may cause overflow, e.g `x as u8` where `x: u32`, or `x as i32` where `x: f32`
|
||||
cast_precision_loss | allow | casts that cause loss of precision, e.g `x as f32` where `x: u64`
|
||||
cast_sign_loss | allow | casts from signed types to unsigned types, e.g `x as u32` where `x: i32`
|
||||
cmp_nan | deny | comparisons to NAN (which will always return false, which is probably not intended)
|
||||
cmp_owned | warn | creating owned instances for comparing with others, e.g. `x == "foo".to_string()`
|
||||
collapsible_if | warn | two nested `if`-expressions can be collapsed into one, e.g. `if x { if y { foo() } }` can be written as `if x && y { foo() }`
|
||||
eq_op | warn | equal operands on both sides of a comparison or bitwise combination (e.g. `x == x`)
|
||||
explicit_iter_loop | warn | for-looping over `_.iter()` or `_.iter_mut()` when `&_` or `&mut _` would do
|
||||
float_cmp | warn | using `==` or `!=` on float values (as floating-point operations usually involve rounding errors, it is always better to check for approximate equality within small bounds)
|
||||
identity_op | warn | using identity operations, e.g. `x + 0` or `y / 1`
|
||||
ineffective_bit_mask | warn | expressions where a bit mask will be rendered useless by a comparison, e.g. `(x | 1) > 2`
|
||||
inline_always | warn | `#[inline(always)]` is a bad idea in most cases
|
||||
iter_next_loop | warn | for-looping over `_.next()` which is probably not intended
|
||||
len_without_is_empty | warn | traits and impls that have `.len()` but not `.is_empty()`
|
||||
len_zero | warn | checking `.len() == 0` or `.len() > 0` (or similar) when `.is_empty()` could be used instead
|
||||
let_and_return | warn | creating a let-binding and then immediately returning it like `let x = expr; x` at the end of a function
|
||||
let_unit_value | warn | creating a let binding to a value of unit type, which usually can't be used afterwards
|
||||
linkedlist | warn | usage of LinkedList, usually a vector is faster, or a more specialized data structure like a RingBuf
|
||||
modulo_one | warn | taking a number modulo 1, which always returns 0
|
||||
mut_mut | warn | usage of double-mut refs, e.g. `&mut &mut ...` (either copy'n'paste error, or shows a fundamental misunderstanding of references)
|
||||
needless_bool | warn | if-statements with plain booleans in the then- and else-clause, e.g. `if p { true } else { false }`
|
||||
needless_lifetimes | warn | using explicit lifetimes for references in function arguments when elision rules would allow omitting them
|
||||
needless_range_loop | warn | for-looping over a range of indices where an iterator over items would do
|
||||
needless_return | warn | using a return statement like `return expr;` where an expression would suffice
|
||||
non_ascii_literal | allow | using any literal non-ASCII chars in a string literal; suggests using the \\u escape instead
|
||||
option_unwrap_used | allow | using `Option.unwrap()`, which should at least get a better message using `expect()`
|
||||
precedence | warn | expressions where precedence may trip up the unwary reader of the source; suggests adding parentheses, e.g. `x << 2 + y` will be parsed as `x << (2 + y)`
|
||||
ptr_arg | allow | fn arguments of the type `&Vec<...>` or `&String`, suggesting to use `&[...]` or `&str` instead, respectively
|
||||
range_step_by_zero | warn | using Range::step_by(0), which produces an infinite iterator
|
||||
redundant_closure | warn | using redundant closures, i.e. `|a| foo(a)` (which can be written as just `foo`)
|
||||
result_unwrap_used | allow | using `Result.unwrap()`, which might be better handled
|
||||
single_match | warn | a match statement with a single nontrivial arm (i.e, where the other arm is `_ => {}`) is used; recommends `if let` instead
|
||||
str_to_string | warn | using `to_string()` on a str, which should be `to_owned()`
|
||||
string_add | allow | using `x + ..` where x is a `String`; suggests using `push_str()` instead
|
||||
string_add_assign | allow | using `x = x + ..` where x is a `String`; suggests using `push_str()` instead
|
||||
string_to_string | warn | calling `String.to_string()` which is a no-op
|
||||
toplevel_ref_arg | warn | a function argument is declared `ref` (i.e. `fn foo(ref x: u8)`, but not `fn foo((ref x, ref y): (u8, u8))`)
|
||||
unit_cmp | warn | comparing unit values (which is always `true` or `false`, respectively)
|
||||
zero_width_space | deny | using a zero-width space in a string literal, which is confusing
|
||||
name | default | meaning
|
||||
-------------------------|---------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
approx_constant | warn | the approximate of a known float constant (in `std::f64::consts` or `std::f32::consts`) is found; suggests to use the constant
|
||||
bad_bit_mask | deny | expressions of the form `_ & mask == select` that will only ever return `true` or `false` (because in the example `select` containing bits that `mask` doesn't have)
|
||||
box_vec | warn | usage of `Box<Vec<T>>`, vector elements are already on the heap
|
||||
cast_possible_truncation | allow | casts that may cause truncation of the value, e.g `x as u8` where `x: u32`, or `x as i32` where `x: f32`
|
||||
cast_precision_loss | allow | casts that cause loss of precision, e.g `x as f32` where `x: u64`
|
||||
cast_sign_loss | allow | casts from signed types to unsigned types, e.g `x as u32` where `x: i32`
|
||||
cmp_nan | deny | comparisons to NAN (which will always return false, which is probably not intended)
|
||||
cmp_owned | warn | creating owned instances for comparing with others, e.g. `x == "foo".to_string()`
|
||||
collapsible_if | warn | two nested `if`-expressions can be collapsed into one, e.g. `if x { if y { foo() } }` can be written as `if x && y { foo() }`
|
||||
eq_op | warn | equal operands on both sides of a comparison or bitwise combination (e.g. `x == x`)
|
||||
explicit_iter_loop | warn | for-looping over `_.iter()` or `_.iter_mut()` when `&_` or `&mut _` would do
|
||||
float_cmp | warn | using `==` or `!=` on float values (as floating-point operations usually involve rounding errors, it is always better to check for approximate equality within small bounds)
|
||||
identity_op | warn | using identity operations, e.g. `x + 0` or `y / 1`
|
||||
ineffective_bit_mask | warn | expressions where a bit mask will be rendered useless by a comparison, e.g. `(x | 1) > 2`
|
||||
inline_always | warn | `#[inline(always)]` is a bad idea in most cases
|
||||
iter_next_loop | warn | for-looping over `_.next()` which is probably not intended
|
||||
len_without_is_empty | warn | traits and impls that have `.len()` but not `.is_empty()`
|
||||
len_zero | warn | checking `.len() == 0` or `.len() > 0` (or similar) when `.is_empty()` could be used instead
|
||||
let_and_return | warn | creating a let-binding and then immediately returning it like `let x = expr; x` at the end of a function
|
||||
let_unit_value | warn | creating a let binding to a value of unit type, which usually can't be used afterwards
|
||||
linkedlist | warn | usage of LinkedList, usually a vector is faster, or a more specialized data structure like a RingBuf
|
||||
modulo_one | warn | taking a number modulo 1, which always returns 0
|
||||
mut_mut | warn | usage of double-mut refs, e.g. `&mut &mut ...` (either copy'n'paste error, or shows a fundamental misunderstanding of references)
|
||||
needless_bool | warn | if-statements with plain booleans in the then- and else-clause, e.g. `if p { true } else { false }`
|
||||
needless_lifetimes | warn | using explicit lifetimes for references in function arguments when elision rules would allow omitting them
|
||||
needless_range_loop | warn | for-looping over a range of indices where an iterator over items would do
|
||||
needless_return | warn | using a return statement like `return expr;` where an expression would suffice
|
||||
non_ascii_literal | allow | using any literal non-ASCII chars in a string literal; suggests using the \\u escape instead
|
||||
option_unwrap_used | allow | using `Option.unwrap()`, which should at least get a better message using `expect()`
|
||||
precedence | warn | expressions where precedence may trip up the unwary reader of the source; suggests adding parentheses, e.g. `x << 2 + y` will be parsed as `x << (2 + y)`
|
||||
ptr_arg | allow | fn arguments of the type `&Vec<...>` or `&String`, suggesting to use `&[...]` or `&str` instead, respectively
|
||||
range_step_by_zero | warn | using Range::step_by(0), which produces an infinite iterator
|
||||
redundant_closure | warn | using redundant closures, i.e. `|a| foo(a)` (which can be written as just `foo`)
|
||||
result_unwrap_used | allow | using `Result.unwrap()`, which might be better handled
|
||||
single_match | warn | a match statement with a single nontrivial arm (i.e, where the other arm is `_ => {}`) is used; recommends `if let` instead
|
||||
str_to_string | warn | using `to_string()` on a str, which should be `to_owned()`
|
||||
string_add | allow | using `x + ..` where x is a `String`; suggests using `push_str()` instead
|
||||
string_add_assign | allow | using `x = x + ..` where x is a `String`; suggests using `push_str()` instead
|
||||
string_to_string | warn | calling `String.to_string()` which is a no-op
|
||||
toplevel_ref_arg | warn | a function argument is declared `ref` (i.e. `fn foo(ref x: u8)`, but not `fn foo((ref x, ref y): (u8, u8))`)
|
||||
unit_cmp | warn | comparing unit values (which is always `true` or `false`, respectively)
|
||||
zero_width_space | deny | using a zero-width space in a string literal, which is confusing
|
||||
|
||||
To use, add the following lines to your Cargo.toml:
|
||||
|
||||
|
@ -106,7 +106,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
|
||||
strings::STRING_ADD,
|
||||
strings::STRING_ADD_ASSIGN,
|
||||
types::BOX_VEC,
|
||||
types::CAST_POSSIBLE_OVERFLOW,
|
||||
types::CAST_POSSIBLE_TRUNCATION,
|
||||
types::CAST_PRECISION_LOSS,
|
||||
types::CAST_SIGN_LOSS,
|
||||
types::LET_UNIT_VALUE,
|
||||
|
23
src/types.rs
23
src/types.rs
@ -143,14 +143,14 @@ declare_lint!(pub CAST_PRECISION_LOSS, Allow,
|
||||
"casts that cause loss of precision, e.g `x as f32` where `x: u64`");
|
||||
declare_lint!(pub CAST_SIGN_LOSS, Allow,
|
||||
"casts from signed types to unsigned types, e.g `x as u32` where `x: i32`");
|
||||
declare_lint!(pub CAST_POSSIBLE_OVERFLOW, Allow,
|
||||
"casts that may cause overflow, e.g `x as u8` where `x: u32`, or `x as i32` where `x: f32`");
|
||||
declare_lint!(pub CAST_POSSIBLE_TRUNCATION, Allow,
|
||||
"casts that may cause truncation of the value, e.g `x as u8` where `x: u32`, or `x as i32` where `x: f32`");
|
||||
|
||||
impl LintPass for CastPass {
|
||||
fn get_lints(&self) -> LintArray {
|
||||
lint_array!(CAST_PRECISION_LOSS,
|
||||
CAST_SIGN_LOSS,
|
||||
CAST_POSSIBLE_OVERFLOW)
|
||||
CAST_POSSIBLE_TRUNCATION)
|
||||
}
|
||||
|
||||
fn check_expr(&mut self, cx: &Context, expr: &Expr) {
|
||||
@ -170,17 +170,18 @@ impl LintPass for CastPass {
|
||||
_ => 0
|
||||
};
|
||||
if from_nbits != 4 {
|
||||
// Handle TyIs/TyUs separately (size is arch dependant)
|
||||
// Handle TyIs/TyUs separately (pointer size is arch dependant)
|
||||
if from_nbits >= to_nbits {
|
||||
span_lint(cx, CAST_PRECISION_LOSS, expr.span,
|
||||
&format!("converting from {} to {}, which causes a loss of precision",
|
||||
cast_from, cast_to));
|
||||
&format!("converting from {0} to {1}, which causes a loss of precision \
|
||||
({0} is {2} bits wide, but {1}'s mantissa is only {3} bits wide)",
|
||||
cast_from, cast_to, from_nbits, if to_nbits == 64 {52} else {23} ));
|
||||
}
|
||||
}
|
||||
},
|
||||
(false, true) => {
|
||||
span_lint(cx, CAST_POSSIBLE_OVERFLOW, expr.span,
|
||||
&format!("the contents of a {} may overflow a {}", cast_from, cast_to));
|
||||
span_lint(cx, CAST_POSSIBLE_TRUNCATION, expr.span,
|
||||
&format!("casting {} to {} may cause truncation of the value", cast_from, cast_to));
|
||||
if !cast_to.is_signed() {
|
||||
span_lint(cx, CAST_SIGN_LOSS, expr.span,
|
||||
&format!("casting from {} to {} loses the sign of the value", cast_from, cast_to));
|
||||
@ -203,14 +204,14 @@ impl LintPass for CastPass {
|
||||
};
|
||||
if to_nbits < from_nbits ||
|
||||
(!cast_from.is_signed() && cast_to.is_signed() && to_nbits <= from_nbits) {
|
||||
span_lint(cx, CAST_POSSIBLE_OVERFLOW, expr.span,
|
||||
&format!("the contents of a {} may overflow a {}", cast_from, cast_to));
|
||||
span_lint(cx, CAST_POSSIBLE_TRUNCATION, expr.span,
|
||||
&format!("casting {} to {} may cause truncation of the value", cast_from, cast_to));
|
||||
}
|
||||
}
|
||||
(false, false) => {
|
||||
if let (&ty::TyFloat(ast::TyF64),
|
||||
&ty::TyFloat(ast::TyF32)) = (&cast_from.sty, &cast_to.sty) {
|
||||
span_lint(cx, CAST_POSSIBLE_OVERFLOW, expr.span, "the contents of a f64 may overflow a f32");
|
||||
span_lint(cx, CAST_POSSIBLE_TRUNCATION, expr.span, "casting f64 to f32 may cause truncation of the value");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,30 +1,30 @@
|
||||
#![feature(plugin)]
|
||||
#![plugin(clippy)]
|
||||
|
||||
#[deny(cast_precision_loss, cast_possible_overflow, cast_sign_loss)]
|
||||
#[deny(cast_precision_loss, cast_possible_truncation, cast_sign_loss)]
|
||||
fn main() {
|
||||
let i : i32 = 42;
|
||||
let u : u32 = 42;
|
||||
let f : f32 = 42.0;
|
||||
|
||||
// Test cast_precision_loss
|
||||
i as f32; //~ERROR converting from i32 to f32, which causes a loss of precision
|
||||
(i as i64) as f32; //~ERROR converting from i64 to f32, which causes a loss of precision
|
||||
(i as i64) as f64; //~ERROR converting from i64 to f64, which causes a loss of precision
|
||||
u as f32; //~ERROR converting from u32 to f32, which causes a loss of precision
|
||||
(u as u64) as f32; //~ERROR converting from u64 to f32, which causes a loss of precision
|
||||
(u as u64) as f64; //~ERROR converting from u64 to f64, which causes a loss of precision
|
||||
i as f32; //~ERROR converting from i32 to f32, which causes a loss of precision (i32 is 32 bits wide, but f32's mantissa is only 23 bits wide)
|
||||
(i as i64) as f32; //~ERROR converting from i64 to f32, which causes a loss of precision (i64 is 64 bits wide, but f32's mantissa is only 23 bits wide)
|
||||
(i as i64) as f64; //~ERROR converting from i64 to f64, which causes a loss of precision (i64 is 64 bits wide, but f64's mantissa is only 52 bits wide)
|
||||
u as f32; //~ERROR converting from u32 to f32, which causes a loss of precision (u32 is 32 bits wide, but f32's mantissa is only 23 bits wide)
|
||||
(u as u64) as f32; //~ERROR converting from u64 to f32, which causes a loss of precision (u64 is 64 bits wide, but f32's mantissa is only 23 bits wide)
|
||||
(u as u64) as f64; //~ERROR converting from u64 to f64, which causes a loss of precision (u64 is 64 bits wide, but f64's mantissa is only 52 bits wide)
|
||||
i as f64; // Should not trigger the lint
|
||||
u as f64; // Should not trigger the lint
|
||||
|
||||
// Test cast_possible_overflow
|
||||
f as i32; //~ERROR the contents of a f32 may overflow a i32
|
||||
f as u32; //~ERROR the contents of a f32 may overflow a u32
|
||||
// Test cast_possible_truncation
|
||||
f as i32; //~ERROR casting f32 to i32 may cause truncation of the value
|
||||
f as u32; //~ERROR casting f32 to u32 may cause truncation of the value
|
||||
//~^ERROR casting from f32 to u32 loses the sign of the value
|
||||
i as u8; //~ERROR the contents of a i32 may overflow a u8
|
||||
i as u8; //~ERROR casting i32 to u8 may cause truncation of the value
|
||||
//~^ERROR casting from i32 to u8 loses the sign of the value
|
||||
(f as f64) as f32; //~ERROR the contents of a f64 may overflow a f32
|
||||
i as i8; //~ERROR the contents of a i32 may overflow a i8
|
||||
(f as f64) as f32; //~ERROR casting f64 to f32 may cause truncation of the value
|
||||
i as i8; //~ERROR casting i32 to i8 may cause truncation of the value
|
||||
|
||||
// Test cast_sign_loss
|
||||
i as u32; //~ERROR casting from i32 to u32 loses the sign of the value
|
||||
|
Loading…
Reference in New Issue
Block a user