1 | // RUN: %check_clang_tidy -std=c++17-or-later %s bugprone-optional-value-conversion %t |
2 | |
3 | namespace std { |
4 | template <typename T> struct optional { |
5 | constexpr optional() noexcept; |
6 | constexpr optional(T &&) noexcept; |
7 | constexpr optional(const T &) noexcept; |
8 | template <typename U> constexpr optional(U &&) noexcept; |
9 | const T &operator*() const; |
10 | T *operator->(); |
11 | const T *operator->() const; |
12 | T &operator*(); |
13 | const T &value() const; |
14 | T &value(); |
15 | const T &get() const; |
16 | T &get(); |
17 | T value_or(T) const; |
18 | }; |
19 | |
20 | template <class T> T &&move(T &x) { return static_cast<T &&>(x); } |
21 | |
22 | template <typename T> class default_delete {}; |
23 | |
24 | template <typename type, typename Deleter = std::default_delete<type>> |
25 | class unique_ptr {}; |
26 | |
27 | template <typename type> |
28 | class shared_ptr {}; |
29 | |
30 | template <typename T> |
31 | class initializer_list {}; |
32 | |
33 | template <class T, class... Args> unique_ptr<T> make_unique(Args &&...args); |
34 | template <class T, class... Args> shared_ptr<T> make_shared(Args &&...args); |
35 | |
36 | template <class T> |
37 | constexpr std::optional<__decay(T)> make_optional(T &&value); |
38 | template <class T, class... Args> |
39 | constexpr std::optional<T> make_optional(Args &&...args); |
40 | template <class T, class U, class... Args> |
41 | constexpr std::optional<T> make_optional(std::initializer_list<U> il, Args &&...args); |
42 | |
43 | } // namespace std |
44 | |
45 | struct A { |
46 | explicit A (int); |
47 | }; |
48 | std::optional<int> opt; |
49 | |
50 | void invalid() { |
51 | std::make_unique<std::optional<int>>(args&: opt.value()); |
52 | // 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] |
53 | using A = std::optional<int>; |
54 | std::make_unique<A>(args&: opt.value()); |
55 | // 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] |
56 | std::make_shared<std::optional<int>>(args&: opt.value()); |
57 | // 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] |
58 | std::make_optional(value&: opt.value()); |
59 | // 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] |
60 | } |
61 | |
62 | void valid() { |
63 | std::make_unique<A>(args&: opt.value()); |
64 | std::make_shared<A>(args&: opt.value()); |
65 | std::make_optional<int>(args&: opt.value()); |
66 | } |
67 | |