Skip to content

Commit

Permalink
Auto merge of rust-lang#11249 - GuillaumeGomez:ui-tests-annotations, …
Browse files Browse the repository at this point in the history
…r=Centri3,llogiq

Add error annotations in UI tests

As discussed on [zulip](https://rust-lang.zulipchat.com/#narrow/stream/257328-clippy/topic/Improve.20UI.20error.20checks), this PR adds missing error annotations in UI tests.

I used this script to generate them:

<details>

```python
import os

def handle_err(line, stderr, elems, kind, i):
    msg = line.split("{}: ".format(kind), 1)[1]
    i += 1
    try:
        line_nb = int(stderr[i].split(":")[1])
    except Exception:
        return i
    in_macro = False
    note_found = False
    help_found = False
    elem = {"kind": kind, "msg": msg, "line": line_nb, "notes": [], "helps": []}
    while i < len(stderr):
        if len(stderr[i]) == 0:
            break
        elif stderr[i].startswith("note:"):
            note_found = True # error checker doesn't like multi-note apparently.
        elif stderr[i].startswith("   = note:"):
            if not note_found and not help_found and "this error originates in the macro" not in stderr[i]:
                elem["notes"].append(stderr[i].split("   = note:", 1)[1].strip())
            note_found = True # error checker doesn't like multi-note apparently.
        elif stderr[i].startswith("   = help:") or stderr[i].startswith("help:"):
            help_found = True
        # elif stderr[i].startswith("help:"):
        #     if not help_found:
        #         elem["helps"].append(stderr[i].split("help:", 1)[1].strip())
        #         help_found = True # error checker doesn't like multi-help apparently.
        elif "in this macro invocation" in stderr[i]:
            in_macro = True
        i += 1
    if not in_macro:
        elems.append(elem)
    return i

def show_kind(kind):
    if kind == "error":
        return "ERROR"
    elif kind == "warning":
        return "WARNING"
    elif kind == "note":
        return "NOTE"
    return "HELP"

def generate_code_err(indent, elem, up):
    content = "{}//~{} {}: {}".format(indent, up, show_kind(elem["kind"]), elem["msg"])
    if up == "^":
        up = "|"
    for note in elem["notes"]:
        content += "\n{}//~{} {}: {}".format(indent, up, show_kind("note"), note)
    for help_msg in elem["helps"]:
        content += "\n{}//~{} {}: {}".format(indent, up, show_kind("help"), help_msg)
    return content, up

def update_content(p, content):
    TO_IGNORE = [
        "needless_bool/simple.rs", # rust-lang/rust-clippy#11248
        "crashes/ice-7868.rs", # Has errors but in another file.
        "trivially_copy_pass_by_ref.rs", # the `N` in the stderr needs to be replaced by the number
        "tests/ui/large_types_passed_by_value.rs", # the `N` in the stderr needs to be replaced by the number
    ]
    for to_ignore in TO_IGNORE:
        if p.endswith(to_ignore):
            return
    try:
        with open(p.replace(".rs", ".stderr"), "r", encoding="utf8") as f:
            stderr = f.read().split('\n')
    except Exception:
        return
    print("Updating `{}`".format(p))
    i = 0
    elems = []
    while i < len(stderr):
        line = stderr[i]
        if line.startswith("error: ") and not line.startswith("error: aborting due to"):
            i = handle_err(line, stderr, elems, "error", i)
        elif line.startswith("warning: ") and not line.endswith("warning emitted") and line.endswith("warnings emitted"):
            i = handle_err(line, stderr, elems, "warning", i)
        i += 1
    elems.sort(key=lambda e: e["line"], reverse=True)
    i = 0
    while i < len(elems):
        elem = elems[i]
        indent = ""
        c = 0
        line = content[elem["line"] - 1]
        while c < len(line) and line[c] == ' ':
            indent += " "
            c += 1
        new_content, up = generate_code_err(indent, elem, "^")
        i += 1
        while i < len(elems) and elems[i]["line"] == elem["line"]:
            elem = elems[i]
            ret = generate_code_err(indent, elem, up)
            new_content += "\n" + ret[0]
            up = ret[1]
            i += 1
        content.insert(elem["line"], new_content)
    with open(p, "w", encoding="utf8") as f:
        f.write("\n".join(content))

def check_if_contains_ui_test(p):
    if not p.endswith(".rs"):
        return
    with open(p, "r", encoding="utf8") as f:
        x = f.read()
    if "//~" not in x and "`@run-rustfix"` not in x and "`@aux-build"` not in x:
        update_content(p, x.split("\n"))

for path, subdirs, files in os.walk("tests/ui"):
    for name in files:
        check_if_contains_ui_test(os.path.join(path, name))
```

</details>

Then ran `cargo uibless`.

changelog: none
  • Loading branch information
bors committed Aug 22, 2023
2 parents fc1152a + f467012 commit 4be90d0
Show file tree
Hide file tree
Showing 733 changed files with 7,564 additions and 3,326 deletions.
19 changes: 19 additions & 0 deletions tests/ui/absurd-extreme-comparisons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,46 @@ fn main() {
const Z: u32 = 0;
let u: u32 = 42;
u <= 0;
//~^ ERROR: this comparison involving the minimum or maximum element for this type con
u <= Z;
//~^ ERROR: this comparison involving the minimum or maximum element for this type con
u < Z;
//~^ ERROR: this comparison involving the minimum or maximum element for this type con
Z >= u;
//~^ ERROR: this comparison involving the minimum or maximum element for this type con
Z > u;
//~^ ERROR: this comparison involving the minimum or maximum element for this type con
u > u32::MAX;
//~^ ERROR: this comparison involving the minimum or maximum element for this type con
u >= u32::MAX;
//~^ ERROR: this comparison involving the minimum or maximum element for this type con
u32::MAX < u;
//~^ ERROR: this comparison involving the minimum or maximum element for this type con
u32::MAX <= u;
//~^ ERROR: this comparison involving the minimum or maximum element for this type con
1-1 > u;
//~^ ERROR: this comparison involving the minimum or maximum element for this type con
u >= !0;
//~^ ERROR: this comparison involving the minimum or maximum element for this type con
u <= 12 - 2*6;
//~^ ERROR: this comparison involving the minimum or maximum element for this type con
let i: i8 = 0;
i < -127 - 1;
//~^ ERROR: this comparison involving the minimum or maximum element for this type con
i8::MAX >= i;
//~^ ERROR: this comparison involving the minimum or maximum element for this type con
3-7 < i32::MIN;
//~^ ERROR: this comparison involving the minimum or maximum element for this type con
let b = false;
b >= true;
//~^ ERROR: this comparison involving the minimum or maximum element for this type con
false > b;
//~^ ERROR: this comparison involving the minimum or maximum element for this type con
u > 0; // ok
// this is handled by clippy::unit_cmp
() < {};
//~^ ERROR: <-comparison of unit values detected. This will always be false
//~| NOTE: `#[deny(clippy::unit_cmp)]` on by default
}

use std::cmp::{Ordering, PartialEq, PartialOrd};
Expand Down
34 changes: 17 additions & 17 deletions tests/ui/absurd-extreme-comparisons.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,135 +8,135 @@ LL | u <= 0;
= note: `-D clippy::absurd-extreme-comparisons` implied by `-D warnings`

error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
--> $DIR/absurd-extreme-comparisons.rs:15:5
--> $DIR/absurd-extreme-comparisons.rs:16:5
|
LL | u <= Z;
| ^^^^^^
|
= help: because `Z` is the minimum value for this type, the case where the two sides are not equal never occurs, consider using `u == Z` instead

error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
--> $DIR/absurd-extreme-comparisons.rs:16:5
--> $DIR/absurd-extreme-comparisons.rs:18:5
|
LL | u < Z;
| ^^^^^
|
= help: because `Z` is the minimum value for this type, this comparison is always false

error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
--> $DIR/absurd-extreme-comparisons.rs:17:5
--> $DIR/absurd-extreme-comparisons.rs:20:5
|
LL | Z >= u;
| ^^^^^^
|
= help: because `Z` is the minimum value for this type, the case where the two sides are not equal never occurs, consider using `Z == u` instead

error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
--> $DIR/absurd-extreme-comparisons.rs:18:5
--> $DIR/absurd-extreme-comparisons.rs:22:5
|
LL | Z > u;
| ^^^^^
|
= help: because `Z` is the minimum value for this type, this comparison is always false

error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
--> $DIR/absurd-extreme-comparisons.rs:19:5
--> $DIR/absurd-extreme-comparisons.rs:24:5
|
LL | u > u32::MAX;
| ^^^^^^^^^^^^
|
= help: because `u32::MAX` is the maximum value for this type, this comparison is always false

error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
--> $DIR/absurd-extreme-comparisons.rs:20:5
--> $DIR/absurd-extreme-comparisons.rs:26:5
|
LL | u >= u32::MAX;
| ^^^^^^^^^^^^^
|
= help: because `u32::MAX` is the maximum value for this type, the case where the two sides are not equal never occurs, consider using `u == u32::MAX` instead

error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
--> $DIR/absurd-extreme-comparisons.rs:21:5
--> $DIR/absurd-extreme-comparisons.rs:28:5
|
LL | u32::MAX < u;
| ^^^^^^^^^^^^
|
= help: because `u32::MAX` is the maximum value for this type, this comparison is always false

error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
--> $DIR/absurd-extreme-comparisons.rs:22:5
--> $DIR/absurd-extreme-comparisons.rs:30:5
|
LL | u32::MAX <= u;
| ^^^^^^^^^^^^^
|
= help: because `u32::MAX` is the maximum value for this type, the case where the two sides are not equal never occurs, consider using `u32::MAX == u` instead

error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
--> $DIR/absurd-extreme-comparisons.rs:23:5
--> $DIR/absurd-extreme-comparisons.rs:32:5
|
LL | 1-1 > u;
| ^^^^^^^
|
= help: because `1-1` is the minimum value for this type, this comparison is always false

error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
--> $DIR/absurd-extreme-comparisons.rs:24:5
--> $DIR/absurd-extreme-comparisons.rs:34:5
|
LL | u >= !0;
| ^^^^^^^
|
= help: because `!0` is the maximum value for this type, the case where the two sides are not equal never occurs, consider using `u == !0` instead

error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
--> $DIR/absurd-extreme-comparisons.rs:25:5
--> $DIR/absurd-extreme-comparisons.rs:36:5
|
LL | u <= 12 - 2*6;
| ^^^^^^^^^^^^^
|
= help: because `12 - 2*6` is the minimum value for this type, the case where the two sides are not equal never occurs, consider using `u == 12 - 2*6` instead

error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
--> $DIR/absurd-extreme-comparisons.rs:27:5
--> $DIR/absurd-extreme-comparisons.rs:39:5
|
LL | i < -127 - 1;
| ^^^^^^^^^^^^
|
= help: because `-127 - 1` is the minimum value for this type, this comparison is always false

error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
--> $DIR/absurd-extreme-comparisons.rs:28:5
--> $DIR/absurd-extreme-comparisons.rs:41:5
|
LL | i8::MAX >= i;
| ^^^^^^^^^^^^
|
= help: because `i8::MAX` is the maximum value for this type, this comparison is always true

error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
--> $DIR/absurd-extreme-comparisons.rs:29:5
--> $DIR/absurd-extreme-comparisons.rs:43:5
|
LL | 3-7 < i32::MIN;
| ^^^^^^^^^^^^^^
|
= help: because `i32::MIN` is the minimum value for this type, this comparison is always false

error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
--> $DIR/absurd-extreme-comparisons.rs:31:5
--> $DIR/absurd-extreme-comparisons.rs:46:5
|
LL | b >= true;
| ^^^^^^^^^
|
= help: because `true` is the maximum value for this type, the case where the two sides are not equal never occurs, consider using `b == true` instead

error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
--> $DIR/absurd-extreme-comparisons.rs:32:5
--> $DIR/absurd-extreme-comparisons.rs:48:5
|
LL | false > b;
| ^^^^^^^^^
|
= help: because `false` is the minimum value for this type, this comparison is always false

error: <-comparison of unit values detected. This will always be false
--> $DIR/absurd-extreme-comparisons.rs:35:5
--> $DIR/absurd-extreme-comparisons.rs:52:5
|
LL | () < {};
| ^^^^^^^
Expand Down
23 changes: 23 additions & 0 deletions tests/ui/approx_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,86 @@
#[allow(clippy::similar_names)]
fn main() {
let my_e = 2.7182;
//~^ ERROR: approximate value of `f{32, 64}::consts::E` found
let almost_e = 2.718;
//~^ ERROR: approximate value of `f{32, 64}::consts::E` found
let no_e = 2.71;

let my_1_frac_pi = 0.3183;
//~^ ERROR: approximate value of `f{32, 64}::consts::FRAC_1_PI` found
let no_1_frac_pi = 0.31;

let my_frac_1_sqrt_2 = 0.70710678;
//~^ ERROR: approximate value of `f{32, 64}::consts::FRAC_1_SQRT_2` found
let almost_frac_1_sqrt_2 = 0.70711;
//~^ ERROR: approximate value of `f{32, 64}::consts::FRAC_1_SQRT_2` found
let my_frac_1_sqrt_2 = 0.707;

let my_frac_2_pi = 0.63661977;
//~^ ERROR: approximate value of `f{32, 64}::consts::FRAC_2_PI` found
let no_frac_2_pi = 0.636;

let my_frac_2_sq_pi = 1.128379;
//~^ ERROR: approximate value of `f{32, 64}::consts::FRAC_2_SQRT_PI` found
let no_frac_2_sq_pi = 1.128;

let my_frac_pi_2 = 1.57079632679;
//~^ ERROR: approximate value of `f{32, 64}::consts::FRAC_PI_2` found
let no_frac_pi_2 = 1.5705;

let my_frac_pi_3 = 1.04719755119;
//~^ ERROR: approximate value of `f{32, 64}::consts::FRAC_PI_3` found
let no_frac_pi_3 = 1.047;

let my_frac_pi_4 = 0.785398163397;
//~^ ERROR: approximate value of `f{32, 64}::consts::FRAC_PI_4` found
let no_frac_pi_4 = 0.785;

let my_frac_pi_6 = 0.523598775598;
//~^ ERROR: approximate value of `f{32, 64}::consts::FRAC_PI_6` found
let no_frac_pi_6 = 0.523;

let my_frac_pi_8 = 0.3926990816987;
//~^ ERROR: approximate value of `f{32, 64}::consts::FRAC_PI_8` found
let no_frac_pi_8 = 0.392;

let my_ln_10 = 2.302585092994046;
//~^ ERROR: approximate value of `f{32, 64}::consts::LN_10` found
let no_ln_10 = 2.303;

let my_ln_2 = 0.6931471805599453;
//~^ ERROR: approximate value of `f{32, 64}::consts::LN_2` found
let no_ln_2 = 0.693;

let my_log10_e = 0.4342944819032518;
//~^ ERROR: approximate value of `f{32, 64}::consts::LOG10_E` found
let no_log10_e = 0.434;

let my_log2_e = 1.4426950408889634;
//~^ ERROR: approximate value of `f{32, 64}::consts::LOG2_E` found
let no_log2_e = 1.442;

let log2_10 = 3.321928094887362;
//~^ ERROR: approximate value of `f{32, 64}::consts::LOG2_10` found
let no_log2_10 = 3.321;

let log10_2 = 0.301029995663981;
//~^ ERROR: approximate value of `f{32, 64}::consts::LOG10_2` found
let no_log10_2 = 0.301;

let my_pi = 3.1415;
//~^ ERROR: approximate value of `f{32, 64}::consts::PI` found
let almost_pi = 3.14;
//~^ ERROR: approximate value of `f{32, 64}::consts::PI` found
let no_pi = 3.15;

let my_sq2 = 1.4142;
//~^ ERROR: approximate value of `f{32, 64}::consts::SQRT_2` found
let no_sq2 = 1.414;

let my_tau = 6.2832;
//~^ ERROR: approximate value of `f{32, 64}::consts::TAU` found
let almost_tau = 6.28;
//~^ ERROR: approximate value of `f{32, 64}::consts::TAU` found
let no_tau = 6.3;
}
Loading

0 comments on commit 4be90d0

Please sign in to comment.