]> git.lizzy.rs Git - rust.git/blob - tests/ui/floating_point_abs.fixed
Ignore associated items in trait *implementations* when considering type complexity
[rust.git] / tests / ui / floating_point_abs.fixed
1 // run-rustfix
2 #![feature(const_fn_floating_point_arithmetic)]
3 #![warn(clippy::suboptimal_flops)]
4
5 /// Allow suboptimal ops in constant context
6 pub const fn in_const_context(num: f64) -> f64 {
7     if num >= 0.0 { num } else { -num }
8 }
9
10 struct A {
11     a: f64,
12     b: f64,
13 }
14
15 fn fake_abs1(num: f64) -> f64 {
16     num.abs()
17 }
18
19 fn fake_abs2(num: f64) -> f64 {
20     num.abs()
21 }
22
23 fn fake_abs3(a: A) -> f64 {
24     a.a.abs()
25 }
26
27 fn fake_abs4(num: f64) -> f64 {
28     num.abs()
29 }
30
31 fn fake_abs5(a: A) -> f64 {
32     a.a.abs()
33 }
34
35 fn fake_nabs1(num: f64) -> f64 {
36     -num.abs()
37 }
38
39 fn fake_nabs2(num: f64) -> f64 {
40     -num.abs()
41 }
42
43 fn fake_nabs3(a: A) -> A {
44     A {
45         a: -a.a.abs(),
46         b: a.b,
47     }
48 }
49
50 fn not_fake_abs1(num: f64) -> f64 {
51     if num > 0.0 { num } else { -num - 1f64 }
52 }
53
54 fn not_fake_abs2(num: f64) -> f64 {
55     if num > 0.0 { num + 1.0 } else { -(num + 1.0) }
56 }
57
58 fn not_fake_abs3(num1: f64, num2: f64) -> f64 {
59     if num1 > 0.0 { num2 } else { -num2 }
60 }
61
62 fn not_fake_abs4(a: A) -> f64 {
63     if a.a > 0.0 { a.b } else { -a.b }
64 }
65
66 fn not_fake_abs5(a: A) -> f64 {
67     if a.a > 0.0 { a.a } else { -a.b }
68 }
69
70 fn main() {
71     fake_abs1(5.0);
72     fake_abs2(5.0);
73     fake_abs3(A { a: 5.0, b: 5.0 });
74     fake_abs4(5.0);
75     fake_abs5(A { a: 5.0, b: 5.0 });
76     fake_nabs1(5.0);
77     fake_nabs2(5.0);
78     fake_nabs3(A { a: 5.0, b: 5.0 });
79     not_fake_abs1(5.0);
80     not_fake_abs2(5.0);
81     not_fake_abs3(5.0, 5.0);
82     not_fake_abs4(A { a: 5.0, b: 5.0 });
83     not_fake_abs5(A { a: 5.0, b: 5.0 });
84 }