brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.3 KiB · 47bcf84 Raw
52 lines · cpp
1// RUN: %check_clang_tidy %s bugprone-unchecked-optional-access %t -- \2// RUN:   -config="{CheckOptions:  \3// RUN:     {bugprone-unchecked-optional-access.IgnoreSmartPointerDereference: true}}" -- \4// RUN:   -I %S/Inputs/unchecked-optional-access5 6#include "absl/types/optional.h"7 8// Include some basic cases to ensure that IgnoreSmartPointerDereference doesn't9// disable everything. Then check the relevant smart-pointer cases.10 11void unchecked_deref_operator_access(const absl::optional<int> &opt) {12  *opt;13  // CHECK-MESSAGES: :[[@LINE-1]]:4: warning: unchecked access to optional value14}15 16void unchecked_value_access(const absl::optional<int> &opt) {17  opt.value();18  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: unchecked access to optional value [bugprone-unchecked-optional-access]19}20 21struct Foo {22  void foo() const {}23};24 25void unchecked_arrow_operator_access(const absl::optional<Foo> &opt) {26  opt->foo();27  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: unchecked access to optional value28}29 30template <typename T>31struct SmartPtr {32  T& operator*() &;33  T* operator->();34};35 36struct Bar {37  absl::optional<int> opt;38};39 40 41void unchecked_value_access_through_smart_ptr(SmartPtr<absl::optional<int>> s) {42  s->value();43  (*s).value();44 45}46 47void unchecked_value_access_through_smart_ptr_field(SmartPtr<Bar> s) {48  s->opt.value();49  (*s).opt.value();50 51}52