diff --git a/tests/ui/methods.rs b/tests/ui/methods.rs index 54296a74759..4769906e2ff 100644 --- a/tests/ui/methods.rs +++ b/tests/ui/methods.rs @@ -498,46 +498,6 @@ fn clone_on_double_ref() { println!("{:p} {:p}",*y, z); } -fn single_char_pattern() { - let x = "foo"; - x.split("x"); - x.split("xx"); - x.split('x'); - - let y = "x"; - x.split(y); - // Not yet testing for multi-byte characters - // Changing `r.len() == 1` to `r.chars().count() == 1` in `lint_single_char_pattern` - // should have done this but produced an ICE - // - // We may not want to suggest changing these anyway - // See: https://github.com/rust-lang-nursery/rust-clippy/issues/650#issuecomment-184328984 - x.split("ß"); - x.split("ℝ"); - x.split("💣"); - // Can't use this lint for unicode code points which don't fit in a char - x.split("❤️"); - x.contains("x"); - x.starts_with("x"); - x.ends_with("x"); - x.find("x"); - x.rfind("x"); - x.rsplit("x"); - x.split_terminator("x"); - x.rsplit_terminator("x"); - x.splitn(0, "x"); - x.rsplitn(0, "x"); - x.matches("x"); - x.rmatches("x"); - x.match_indices("x"); - x.rmatch_indices("x"); - x.trim_left_matches("x"); - x.trim_right_matches("x"); - - let h = HashSet::::new(); - h.contains("X"); // should not warn -} - #[allow(result_unwrap_used)] fn temporary_cstring() { use std::ffi::CString; diff --git a/tests/ui/methods.stderr b/tests/ui/methods.stderr index d50ef4e5284..2500c30b402 100644 --- a/tests/ui/methods.stderr +++ b/tests/ui/methods.stderr @@ -627,128 +627,24 @@ error: using `clone` on a double-reference; this will copy the reference instead | = note: `-D clone-double-ref` implied by `-D warnings` -error: single-character string constant used as pattern - --> $DIR/methods.rs:503:13 - | -503 | x.split("x"); - | --------^^^- help: try using a char instead: `x.split('x')` - | - = note: `-D single-char-pattern` implied by `-D warnings` - -error: single-character string constant used as pattern - --> $DIR/methods.rs:520:16 - | -520 | x.contains("x"); - | -----------^^^- help: try using a char instead: `x.contains('x')` - -error: single-character string constant used as pattern - --> $DIR/methods.rs:521:19 - | -521 | x.starts_with("x"); - | --------------^^^- help: try using a char instead: `x.starts_with('x')` - -error: single-character string constant used as pattern - --> $DIR/methods.rs:522:17 - | -522 | x.ends_with("x"); - | ------------^^^- help: try using a char instead: `x.ends_with('x')` - -error: single-character string constant used as pattern - --> $DIR/methods.rs:523:12 - | -523 | x.find("x"); - | -------^^^- help: try using a char instead: `x.find('x')` - -error: single-character string constant used as pattern - --> $DIR/methods.rs:524:13 - | -524 | x.rfind("x"); - | --------^^^- help: try using a char instead: `x.rfind('x')` - -error: single-character string constant used as pattern - --> $DIR/methods.rs:525:14 - | -525 | x.rsplit("x"); - | ---------^^^- help: try using a char instead: `x.rsplit('x')` - -error: single-character string constant used as pattern - --> $DIR/methods.rs:526:24 - | -526 | x.split_terminator("x"); - | -------------------^^^- help: try using a char instead: `x.split_terminator('x')` - -error: single-character string constant used as pattern - --> $DIR/methods.rs:527:25 - | -527 | x.rsplit_terminator("x"); - | --------------------^^^- help: try using a char instead: `x.rsplit_terminator('x')` - -error: single-character string constant used as pattern - --> $DIR/methods.rs:528:17 - | -528 | x.splitn(0, "x"); - | ------------^^^- help: try using a char instead: `x.splitn(0, 'x')` - -error: single-character string constant used as pattern - --> $DIR/methods.rs:529:18 - | -529 | x.rsplitn(0, "x"); - | -------------^^^- help: try using a char instead: `x.rsplitn(0, 'x')` - -error: single-character string constant used as pattern - --> $DIR/methods.rs:530:15 - | -530 | x.matches("x"); - | ----------^^^- help: try using a char instead: `x.matches('x')` - -error: single-character string constant used as pattern - --> $DIR/methods.rs:531:16 - | -531 | x.rmatches("x"); - | -----------^^^- help: try using a char instead: `x.rmatches('x')` - -error: single-character string constant used as pattern - --> $DIR/methods.rs:532:21 - | -532 | x.match_indices("x"); - | ----------------^^^- help: try using a char instead: `x.match_indices('x')` - -error: single-character string constant used as pattern - --> $DIR/methods.rs:533:22 - | -533 | x.rmatch_indices("x"); - | -----------------^^^- help: try using a char instead: `x.rmatch_indices('x')` - -error: single-character string constant used as pattern - --> $DIR/methods.rs:534:25 - | -534 | x.trim_left_matches("x"); - | --------------------^^^- help: try using a char instead: `x.trim_left_matches('x')` - -error: single-character string constant used as pattern - --> $DIR/methods.rs:535:26 - | -535 | x.trim_right_matches("x"); - | ---------------------^^^- help: try using a char instead: `x.trim_right_matches('x')` - error: you are getting the inner pointer of a temporary `CString` - --> $DIR/methods.rs:545:5 + --> $DIR/methods.rs:505:5 | -545 | CString::new("foo").unwrap().as_ptr(); +505 | CString::new("foo").unwrap().as_ptr(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `-D temporary-cstring-as-ptr` implied by `-D warnings` = note: that pointer will be invalid outside this expression help: assign the `CString` to a variable to extend its lifetime - --> $DIR/methods.rs:545:5 + --> $DIR/methods.rs:505:5 | -545 | CString::new("foo").unwrap().as_ptr(); +505 | CString::new("foo").unwrap().as_ptr(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: called `cloned().collect()` on a slice to create a `Vec`. Calling `to_vec()` is both faster and more readable - --> $DIR/methods.rs:550:27 + --> $DIR/methods.rs:510:27 | -550 | let v2 : Vec = v.iter().cloned().collect(); +510 | let v2 : Vec = v.iter().cloned().collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `-D iter-cloned-collect` implied by `-D warnings` diff --git a/tests/ui/single_char_pattern.rs b/tests/ui/single_char_pattern.rs new file mode 100644 index 00000000000..948a8ff0e41 --- /dev/null +++ b/tests/ui/single_char_pattern.rs @@ -0,0 +1,41 @@ +use std::collections::HashSet; + +fn main() { + let x = "foo"; + x.split("x"); + x.split("xx"); + x.split('x'); + + let y = "x"; + x.split(y); + // Not yet testing for multi-byte characters + // Changing `r.len() == 1` to `r.chars().count() == 1` in `lint_single_char_pattern` + // should have done this but produced an ICE + // + // We may not want to suggest changing these anyway + // See: https://github.com/rust-lang-nursery/rust-clippy/issues/650#issuecomment-184328984 + x.split("ß"); + x.split("ℝ"); + x.split("💣"); + // Can't use this lint for unicode code points which don't fit in a char + x.split("❤️"); + x.contains("x"); + x.starts_with("x"); + x.ends_with("x"); + x.find("x"); + x.rfind("x"); + x.rsplit("x"); + x.split_terminator("x"); + x.rsplit_terminator("x"); + x.splitn(0, "x"); + x.rsplitn(0, "x"); + x.matches("x"); + x.rmatches("x"); + x.match_indices("x"); + x.rmatch_indices("x"); + x.trim_left_matches("x"); + x.trim_right_matches("x"); + + let h = HashSet::::new(); + h.contains("X"); // should not warn +} diff --git a/tests/ui/single_char_pattern.stderr b/tests/ui/single_char_pattern.stderr new file mode 100644 index 00000000000..d5f21f210a3 --- /dev/null +++ b/tests/ui/single_char_pattern.stderr @@ -0,0 +1,104 @@ +error: single-character string constant used as pattern + --> $DIR/single_char_pattern.rs:5:13 + | +5 | x.split("x"); + | --------^^^- help: try using a char instead: `x.split('x')` + | + = note: `-D single-char-pattern` implied by `-D warnings` + +error: single-character string constant used as pattern + --> $DIR/single_char_pattern.rs:22:16 + | +22 | x.contains("x"); + | -----------^^^- help: try using a char instead: `x.contains('x')` + +error: single-character string constant used as pattern + --> $DIR/single_char_pattern.rs:23:19 + | +23 | x.starts_with("x"); + | --------------^^^- help: try using a char instead: `x.starts_with('x')` + +error: single-character string constant used as pattern + --> $DIR/single_char_pattern.rs:24:17 + | +24 | x.ends_with("x"); + | ------------^^^- help: try using a char instead: `x.ends_with('x')` + +error: single-character string constant used as pattern + --> $DIR/single_char_pattern.rs:25:12 + | +25 | x.find("x"); + | -------^^^- help: try using a char instead: `x.find('x')` + +error: single-character string constant used as pattern + --> $DIR/single_char_pattern.rs:26:13 + | +26 | x.rfind("x"); + | --------^^^- help: try using a char instead: `x.rfind('x')` + +error: single-character string constant used as pattern + --> $DIR/single_char_pattern.rs:27:14 + | +27 | x.rsplit("x"); + | ---------^^^- help: try using a char instead: `x.rsplit('x')` + +error: single-character string constant used as pattern + --> $DIR/single_char_pattern.rs:28:24 + | +28 | x.split_terminator("x"); + | -------------------^^^- help: try using a char instead: `x.split_terminator('x')` + +error: single-character string constant used as pattern + --> $DIR/single_char_pattern.rs:29:25 + | +29 | x.rsplit_terminator("x"); + | --------------------^^^- help: try using a char instead: `x.rsplit_terminator('x')` + +error: single-character string constant used as pattern + --> $DIR/single_char_pattern.rs:30:17 + | +30 | x.splitn(0, "x"); + | ------------^^^- help: try using a char instead: `x.splitn(0, 'x')` + +error: single-character string constant used as pattern + --> $DIR/single_char_pattern.rs:31:18 + | +31 | x.rsplitn(0, "x"); + | -------------^^^- help: try using a char instead: `x.rsplitn(0, 'x')` + +error: single-character string constant used as pattern + --> $DIR/single_char_pattern.rs:32:15 + | +32 | x.matches("x"); + | ----------^^^- help: try using a char instead: `x.matches('x')` + +error: single-character string constant used as pattern + --> $DIR/single_char_pattern.rs:33:16 + | +33 | x.rmatches("x"); + | -----------^^^- help: try using a char instead: `x.rmatches('x')` + +error: single-character string constant used as pattern + --> $DIR/single_char_pattern.rs:34:21 + | +34 | x.match_indices("x"); + | ----------------^^^- help: try using a char instead: `x.match_indices('x')` + +error: single-character string constant used as pattern + --> $DIR/single_char_pattern.rs:35:22 + | +35 | x.rmatch_indices("x"); + | -----------------^^^- help: try using a char instead: `x.rmatch_indices('x')` + +error: single-character string constant used as pattern + --> $DIR/single_char_pattern.rs:36:25 + | +36 | x.trim_left_matches("x"); + | --------------------^^^- help: try using a char instead: `x.trim_left_matches('x')` + +error: single-character string constant used as pattern + --> $DIR/single_char_pattern.rs:37:26 + | +37 | x.trim_right_matches("x"); + | ---------------------^^^- help: try using a char instead: `x.trim_right_matches('x')` +