File: binary-op-suggest-deref.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, trixie
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (83 lines) | stat: -rw-r--r-- 2,867 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#![allow(dead_code)]

fn foo() {
    // Issue #52544
    let i: &i64 = &1;
    if i < 0 {}
    //~^ ERROR mismatched types [E0308]
}

fn bar() {
    // Issue #40660
    let foo = &&0;

    // Dereference LHS
    _ = foo == 0;
    //~^ERROR can't compare `&&{integer}` with `{integer}` [E0277]
    _ = foo == &0;
    //~^ERROR can't compare `&{integer}` with `{integer}` [E0277]
    _ = &&&&foo == 0;
    //~^ERROR can't compare `&&&&&&{integer}` with `{integer}` [E0277]
    _ = *foo == 0;
    //~^ERROR can't compare `&{integer}` with `{integer}` [E0277]
    _ = &&foo == &&0;
    //~^ERROR can't compare `&&{integer}` with `{integer}` [E0277]
    _ = &Box::new(42) == 42;
    //~^ERROR can't compare `&Box<{integer}>` with `{integer}` [E0277]
    _ = &Box::new(&Box::new(&42)) == 42;
    //~^ERROR can't compare `&Box<&Box<&{integer}>>` with `{integer}` [E0277]

    // Dereference RHS
    _ = 0 == foo;
    //~^ERROR can't compare `{integer}` with `&&{integer}` [E0277]
    _ = &0 == foo;
    //~^ERROR can't compare `{integer}` with `&{integer}` [E0277]
    _ = 0 == &&&&foo;
    //~^ERROR can't compare `{integer}` with `&&&&&&{integer}` [E0277]
    _ = 0 == *foo;
    //~^ERROR can't compare `{integer}` with `&{integer}` [E0277]
    _ = &&0 == &&foo;
    //~^ERROR can't compare `{integer}` with `&&{integer}` [E0277]

    // Dereference both sides
    _ = &Box::new(Box::new(42)) == &foo;
    //~^ERROR can't compare `Box<Box<{integer}>>` with `&&{integer}` [E0277]
    _ = &Box::new(42) == &foo;
    //~^ERROR can't compare `Box<{integer}>` with `&&{integer}` [E0277]
    _ = &Box::new(Box::new(Box::new(Box::new(42)))) == &foo;
    //~^ERROR can't compare `Box<Box<Box<Box<{integer}>>>>` with `&&{integer}` [E0277]
    _ = &foo == &Box::new(Box::new(Box::new(Box::new(42))));
    //~^ERROR can't compare `&&{integer}` with `Box<Box<Box<Box<{integer}>>>>` [E0277]

    // Don't suggest dereferencing the LHS; suggest boxing the RHS instead
    _ = Box::new(42) == 42;
    //~^ERROR mismatched types [E0308]

    // Don't suggest dereferencing with types that can't be compared
    struct Foo;
    _ = &&0 == Foo;
    //~^ERROR can't compare `&&{integer}` with `Foo` [E0277]
    _ = Foo == &&0;
    //~^ERROR binary operation `==` cannot be applied to type `Foo` [E0369]
}

fn baz() {
    // Issue #44695
    let owned = "foo".to_owned();
    let string_ref = &owned;
    let partial = "foobar";
    _ = string_ref == partial[..3];
    //~^ERROR can't compare `&String` with `str` [E0277]
    _ = partial[..3] == string_ref;
    //~^ERROR can't compare `str` with `&String` [E0277]
}

fn qux() {
    // Issue #119352
    const FOO: i32 = 42;
    let _ = FOO & (*"Sized".to_string().into_boxed_str());
    //~^ ERROR the size for values of type `str` cannot be known at compilation time
    //~| ERROR no implementation for `i32 & str` [E0277]
}

fn main() {}