File: abseil-duration-unnecessary-conversion.cpp

package info (click to toggle)
llvm-toolchain-9 1%3A9.0.1-16.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 882,388 kB
  • sloc: cpp: 4,167,636; ansic: 714,256; asm: 457,610; python: 155,927; objc: 65,094; sh: 42,856; lisp: 26,908; perl: 7,786; pascal: 7,722; makefile: 6,881; ml: 5,581; awk: 3,648; cs: 2,027; xml: 888; javascript: 381; ruby: 156
file content (111 lines) | stat: -rw-r--r-- 6,313 bytes parent folder | download | duplicates (2)
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
108
109
110
111
// RUN: %check_clang_tidy -std=c++11-or-later %s abseil-duration-unnecessary-conversion %t -- -- -I %S/Inputs

#include "absl/time/time.h"

void f() {
  absl::Duration d1, d2;

  // Floating point
  d2 = absl::Hours(absl::ToDoubleHours(d1));
  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
  // CHECK-FIXES: d2 = d1
  d2 = absl::Minutes(absl::ToDoubleMinutes(d1));
  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
  // CHECK-FIXES: d2 = d1
  d2 = absl::Seconds(absl::ToDoubleSeconds(d1));
  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
  // CHECK-FIXES: d2 = d1
  d2 = absl::Milliseconds(absl::ToDoubleMilliseconds(d1));
  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
  // CHECK-FIXES: d2 = d1
  d2 = absl::Microseconds(absl::ToDoubleMicroseconds(d1));
  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
  // CHECK-FIXES: d2 = d1
  d2 = absl::Nanoseconds(absl::ToDoubleNanoseconds(d1));
  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
  // CHECK-FIXES: d2 = d1

  // Integer point
  d2 = absl::Hours(absl::ToInt64Hours(d1));
  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
  // CHECK-FIXES: d2 = d1
  d2 = absl::Minutes(absl::ToInt64Minutes(d1));
  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
  // CHECK-FIXES: d2 = d1
  d2 = absl::Seconds(absl::ToInt64Seconds(d1));
  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
  // CHECK-FIXES: d2 = d1
  d2 = absl::Milliseconds(absl::ToInt64Milliseconds(d1));
  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
  // CHECK-FIXES: d2 = d1
  d2 = absl::Microseconds(absl::ToInt64Microseconds(d1));
  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
  // CHECK-FIXES: d2 = d1
  d2 = absl::Nanoseconds(absl::ToInt64Nanoseconds(d1));
  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
  // CHECK-FIXES: d2 = d1

  d2 = absl::Hours(d1 / absl::Hours(1));
  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
  // CHECK-FIXES: d2 = d1
  d2 = absl::Minutes(d1 / absl::Minutes(1));
  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
  // CHECK-FIXES: d2 = d1
  d2 = absl::Seconds(d1 / absl::Seconds(1));
  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
  // CHECK-FIXES: d2 = d1
  d2 = absl::Milliseconds(d1 / absl::Milliseconds(1));
  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
  // CHECK-FIXES: d2 = d1
  d2 = absl::Microseconds(d1 / absl::Microseconds(1));
  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
  // CHECK-FIXES: d2 = d1
  d2 = absl::Nanoseconds(d1 / absl::Nanoseconds(1));
  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
  // CHECK-FIXES: d2 = d1

  d2 = absl::Hours(absl::FDivDuration(d1, absl::Hours(1)));
  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
  // CHECK-FIXES: d2 = d1
  d2 = absl::Minutes(absl::FDivDuration(d1, absl::Minutes(1)));
  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
  // CHECK-FIXES: d2 = d1
  d2 = absl::Seconds(absl::FDivDuration(d1, absl::Seconds(1)));
  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
  // CHECK-FIXES: d2 = d1
  d2 = absl::Milliseconds(absl::FDivDuration(d1, absl::Milliseconds(1)));
  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
  // CHECK-FIXES: d2 = d1
  d2 = absl::Microseconds(absl::FDivDuration(d1, absl::Microseconds(1)));
  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
  // CHECK-FIXES: d2 = d1
  d2 = absl::Nanoseconds(absl::FDivDuration(d1, absl::Nanoseconds(1)));
  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
  // CHECK-FIXES: d2 = d1

  // As macro argument
#define PLUS_FIVE_S(x) x + absl::Seconds(5)
  d2 = PLUS_FIVE_S(absl::Seconds(absl::ToInt64Seconds(d1)));
  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
  // CHECK-FIXES: PLUS_FIVE_S(d1)
#undef PLUS_FIVE_S

  // Split by macro: should not change
#define TOSECONDS(x) absl::Seconds(x)
  d2 = TOSECONDS(absl::ToInt64Seconds(d1));
#undef TOSECONDS

  // Don't change something inside a macro definition
#define VALUE(x) absl::Hours(absl::ToInt64Hours(x));
  d2 = VALUE(d1);
#undef VALUE

  // These should not match
  d2 = absl::Seconds(absl::ToDoubleMilliseconds(d1));
  d2 = absl::Seconds(4);
  int i = absl::ToInt64Milliseconds(d1);
  d2 = absl::Hours(d1 / absl::Minutes(1));
  d2 = absl::Seconds(d1 / absl::Seconds(30));
  d2 = absl::Hours(absl::FDivDuration(d1, absl::Minutes(1)));
  d2 = absl::Milliseconds(absl::FDivDuration(d1, absl::Milliseconds(20)));
}