summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/test/clang-tidy/checkers/abseil-duration-factory-float.cpp
blob: 2649d2b90d8e68e5cd2de7a6cea8c7c9e30e4362 (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// RUN: %check_clang_tidy %s abseil-duration-factory-float %t -- -- -I%S/Inputs

#include "absl/time/time.h"

void ConvertFloatTest() {
  absl::Duration d;

  d = absl::Seconds(60.0);
  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: use the integer version of absl::Seconds [abseil-duration-factory-float]
  // CHECK-FIXES: absl::Seconds(60);
  d = absl::Minutes(300.0);
  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: use the integer version of absl::Minutes [abseil-duration-factory-float]
  // CHECK-FIXES: absl::Minutes(300);

  d = absl::Milliseconds(1e2);
  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: use the integer version of absl::Milliseconds [abseil-duration-factory-float]
  // CHECK-FIXES: absl::Milliseconds(100);
  d = absl::Seconds(3.0f);
  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: use the integer version of absl::Seconds [abseil-duration-factory-float]
  // CHECK-FIXES: absl::Seconds(3);
  d = absl::Seconds(3.);
  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: use the integer version of absl::Seconds [abseil-duration-factory-float]
  // CHECK-FIXES: absl::Seconds(3);
  d = absl::Seconds(0x3.p0);
  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: use the integer version of absl::Seconds [abseil-duration-factory-float]
  // CHECK-FIXES: absl::Seconds(3);
  d = absl::Seconds(0x3.p1);
  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: use the integer version of absl::Seconds [abseil-duration-factory-float]
  // CHECK-FIXES: absl::Seconds(6);


  // Ignored expressions
  d = absl::Seconds(.001);
  d = absl::Seconds(.100);
  d = ::absl::Seconds(1);
  d = ::absl::Minutes(1);
  d = ::absl::Hours(1);
  d = absl::Seconds(0x3.4p1);

  // Negative literals (we don't yet handle this case)
  d = absl::Seconds(-3.0);

  // This is bigger than we can safely fit in a positive int32, so we ignore it.
  d = absl::Seconds(1e12);

  int x;
  d = absl::Seconds(x);
  float y;
  d = absl::Minutes(y);

#define SECONDS(x) absl::Seconds(x)
  SECONDS(60);
#undef SECONDS
#define THIRTY 30.0
  d = absl::Seconds(THIRTY);
#undef THIRTY
}

template <int N>
void InTemplate() {
  absl::Duration d;

  d = absl::Seconds(N);  // 1
  // ^ No replacement here.

  d = absl::Minutes(1.0);  // 2
  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: use the integer version of absl::Minutes [abseil-duration-factory-float]
  // CHECK-FIXES: absl::Minutes(1);  // 2
}

void Instantiate() {
  InTemplate<60>();
  InTemplate<1>();
}

void ConvertCastTest() {
  absl::Duration d;

  d = absl::Seconds(static_cast<double>(5));
  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: use the integer version of absl::Seconds [abseil-duration-factory-float]
  // CHECK-FIXES: absl::Seconds(5);

  d = absl::Minutes(static_cast<float>(5));
  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: use the integer version of absl::Minutes [abseil-duration-factory-float]
  // CHECK-FIXES: absl::Minutes(5);

  d = absl::Seconds((double) 5);
  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: use the integer version of absl::Seconds [abseil-duration-factory-float]
  // CHECK-FIXES: absl::Seconds(5);

  d = absl::Minutes((float) 5);
  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: use the integer version of absl::Minutes [abseil-duration-factory-float]
  // CHECK-FIXES: absl::Minutes(5);

  d = absl::Seconds(double(5));
  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: use the integer version of absl::Seconds [abseil-duration-factory-float]
  // CHECK-FIXES: absl::Seconds(5);

  d = absl::Minutes(float(5));
  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: use the integer version of absl::Minutes [abseil-duration-factory-float]
  // CHECK-FIXES: absl::Minutes(5);

  // This should not be flagged
  d = absl::Seconds(static_cast<int>(5.0));
  d = absl::Seconds((int) 5.0);
  d = absl::Seconds(int(5.0));
}
OpenPOWER on IntegriCloud