diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/docs.rs | 5 | ||||
| -rw-r--r-- | src/docs/as_ptr_cast_mut.txt | 19 | ||||
| -rw-r--r-- | src/docs/box_default.txt | 6 | ||||
| -rw-r--r-- | src/docs/cast_nan_to_int.txt | 15 | ||||
| -rw-r--r-- | src/docs/manual_filter.txt | 21 | ||||
| -rw-r--r-- | src/docs/partial_pub_fields.txt | 27 | ||||
| -rw-r--r-- | src/docs/unused_format_specs.txt | 24 |
7 files changed, 111 insertions, 6 deletions
diff --git a/src/docs.rs b/src/docs.rs index bd27bc7938f..0a9fb1ecd84 100644 --- a/src/docs.rs +++ b/src/docs.rs @@ -28,6 +28,7 @@ docs! { "approx_constant", "arithmetic_side_effects", "as_conversions", + "as_ptr_cast_mut", "as_underscore", "assertions_on_constants", "assertions_on_result_states", @@ -60,6 +61,7 @@ docs! { "cast_enum_constructor", "cast_enum_truncation", "cast_lossless", + "cast_nan_to_int", "cast_possible_truncation", "cast_possible_wrap", "cast_precision_loss", @@ -257,6 +259,7 @@ docs! { "manual_async_fn", "manual_bits", "manual_clamp", + "manual_filter", "manual_filter_map", "manual_find", "manual_find_map", @@ -391,6 +394,7 @@ docs! { "panic", "panic_in_result_fn", "panicking_unwrap", + "partial_pub_fields", "partialeq_ne_impl", "partialeq_to_none", "path_buf_push_overwrite", @@ -553,6 +557,7 @@ docs! { "unseparated_literal_suffix", "unsound_collection_transmute", "unused_async", + "unused_format_specs", "unused_io_amount", "unused_peekable", "unused_rounding", diff --git a/src/docs/as_ptr_cast_mut.txt b/src/docs/as_ptr_cast_mut.txt new file mode 100644 index 00000000000..228dde996bb --- /dev/null +++ b/src/docs/as_ptr_cast_mut.txt @@ -0,0 +1,19 @@ +### What it does +Checks for the result of a `&self`-taking `as_ptr` being cast to a mutable pointer + +### Why is this bad? +Since `as_ptr` takes a `&self`, the pointer won't have write permissions unless interior +mutability is used, making it unlikely that having it as a mutable pointer is correct. + +### Example +``` +let string = String::with_capacity(1); +let ptr = string.as_ptr() as *mut u8; +unsafe { ptr.write(4) }; // UNDEFINED BEHAVIOUR +``` +Use instead: +``` +let mut string = String::with_capacity(1); +let ptr = string.as_mut_ptr(); +unsafe { ptr.write(4) }; +``` \ No newline at end of file diff --git a/src/docs/box_default.txt b/src/docs/box_default.txt index ffac894d0c5..1c670c77333 100644 --- a/src/docs/box_default.txt +++ b/src/docs/box_default.txt @@ -7,12 +7,6 @@ First, it's more complex, involving two calls instead of one. Second, `Box::default()` can be faster [in certain cases](https://nnethercote.github.io/perf-book/standard-library-types.html#box). -### Known problems -The lint may miss some cases (e.g. Box::new(String::from(""))). -On the other hand, it will trigger on cases where the `default` -code comes from a macro that does something different based on -e.g. target operating system. - ### Example ``` let x: Box<String> = Box::new(Default::default()); diff --git a/src/docs/cast_nan_to_int.txt b/src/docs/cast_nan_to_int.txt new file mode 100644 index 00000000000..122f5da0c92 --- /dev/null +++ b/src/docs/cast_nan_to_int.txt @@ -0,0 +1,15 @@ +### What it does +Checks for a known NaN float being cast to an integer + +### Why is this bad? +NaNs are cast into zero, so one could simply use this and make the +code more readable. The lint could also hint at a programmer error. + +### Example +``` +let _: (0.0_f32 / 0.0) as u64; +``` +Use instead: +``` +let _: = 0_u64; +``` \ No newline at end of file diff --git a/src/docs/manual_filter.txt b/src/docs/manual_filter.txt new file mode 100644 index 00000000000..19a4d9319d9 --- /dev/null +++ b/src/docs/manual_filter.txt @@ -0,0 +1,21 @@ +### What it does +Checks for usages of `match` which could be implemented using `filter` + +### Why is this bad? +Using the `filter` method is clearer and more concise. + +### Example +``` +match Some(0) { + Some(x) => if x % 2 == 0 { + Some(x) + } else { + None + }, + None => None, +}; +``` +Use instead: +``` +Some(0).filter(|&x| x % 2 == 0); +``` \ No newline at end of file diff --git a/src/docs/partial_pub_fields.txt b/src/docs/partial_pub_fields.txt new file mode 100644 index 00000000000..b529adf1547 --- /dev/null +++ b/src/docs/partial_pub_fields.txt @@ -0,0 +1,27 @@ +### What it does +Checks whether partial fields of a struct are public. + +Either make all fields of a type public, or make none of them public + +### Why is this bad? +Most types should either be: +* Abstract data types: complex objects with opaque implementation which guard +interior invariants and expose intentionally limited API to the outside world. +* Data: relatively simple objects which group a bunch of related attributes together. + +### Example +``` +pub struct Color { + pub r: u8, + pub g: u8, + b: u8, +} +``` +Use instead: +``` +pub struct Color { + pub r: u8, + pub g: u8, + pub b: u8, +} +``` \ No newline at end of file diff --git a/src/docs/unused_format_specs.txt b/src/docs/unused_format_specs.txt new file mode 100644 index 00000000000..77be3a2fb17 --- /dev/null +++ b/src/docs/unused_format_specs.txt @@ -0,0 +1,24 @@ +### What it does +Detects [formatting parameters] that have no effect on the output of +`format!()`, `println!()` or similar macros. + +### Why is this bad? +Shorter format specifiers are easier to read, it may also indicate that +an expected formatting operation such as adding padding isn't happening. + +### Example +``` +println!("{:.}", 1.0); + +println!("not padded: {:5}", format_args!("...")); +``` +Use instead: +``` +println!("{}", 1.0); + +println!("not padded: {}", format_args!("...")); +// OR +println!("padded: {:5}", format!("...")); +``` + +[formatting parameters]: https://doc.rust-lang.org/std/fmt/index.html#formatting-parameters \ No newline at end of file |
