File: optional-value-conversion-construct-from-std.cpp

package info (click to toggle)
llvm-toolchain-20 1%3A20.1.8-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,111,388 kB
  • sloc: cpp: 7,438,767; ansic: 1,393,871; asm: 1,012,926; python: 241,728; f90: 86,635; objc: 75,411; lisp: 42,144; pascal: 17,286; sh: 10,027; ml: 5,082; perl: 4,730; awk: 3,523; makefile: 3,349; javascript: 2,251; xml: 892; fortran: 672
file content (53 lines) | stat: -rw-r--r-- 1,948 bytes parent folder | download
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
// RUN: %check_clang_tidy -std=c++17-or-later %s bugprone-optional-value-conversion %t

namespace std {
template <typename T> struct optional {
  constexpr optional() noexcept;
  constexpr optional(T &&) noexcept;
  constexpr optional(const T &) noexcept;
  template <typename U> constexpr optional(U &&) noexcept;
  const T &operator*() const;
  T *operator->();
  const T *operator->() const;
  T &operator*();
  const T &value() const;
  T &value();
  const T &get() const;
  T &get();
  T value_or(T) const;
};

template <class T> T &&move(T &x) { return static_cast<T &&>(x); }

template <typename T> class default_delete {};

template <typename type, typename Deleter = std::default_delete<type>>
class unique_ptr {};

template <typename type>
class shared_ptr {};

template <class T, class... Args> unique_ptr<T> make_unique(Args &&...args);
template <class T, class... Args> shared_ptr<T> make_shared(Args &&...args);

} // namespace std

struct A {
    explicit A (int);
};
std::optional<int> opt;

void invalid() {
  std::make_unique<std::optional<int>>(opt.value());
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: conversion from 'std::optional<int>' into 'int' and back into 'std::optional<int>', remove potentially error-prone optional dereference [bugprone-optional-value-conversion]
  using A = std::optional<int>;
  std::make_unique<A>(opt.value());
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: conversion from 'std::optional<int>' into 'int' and back into 'std::optional<int>', remove potentially error-prone optional dereference [bugprone-optional-value-conversion]
  std::make_shared<std::optional<int>>(opt.value());
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: conversion from 'std::optional<int>' into 'int' and back into 'std::optional<int>', remove potentially error-prone optional dereference [bugprone-optional-value-conversion]
}

void valid() {
  std::make_unique<A>(opt.value());
  std::make_shared<A>(opt.value());
}