1*6777b538SAndroid Build Coastguard Worker// Copyright 2020 The Chromium Authors 2*6777b538SAndroid Build Coastguard Worker// Use of this source code is governed by a BSD-style license that can be 3*6777b538SAndroid Build Coastguard Worker// found in the LICENSE file. 4*6777b538SAndroid Build Coastguard Worker 5*6777b538SAndroid Build Coastguard Worker// This is a "No Compile Test" suite. 6*6777b538SAndroid Build Coastguard Worker// https://dev.chromium.org/developers/testing/no-compile-tests 7*6777b538SAndroid Build Coastguard Worker 8*6777b538SAndroid Build Coastguard Worker#include <set> 9*6777b538SAndroid Build Coastguard Worker#include <string_view> 10*6777b538SAndroid Build Coastguard Worker 11*6777b538SAndroid Build Coastguard Worker#include "base/containers/contains.h" 12*6777b538SAndroid Build Coastguard Worker 13*6777b538SAndroid Build Coastguard Worker 14*6777b538SAndroid Build Coastguard Worker 15*6777b538SAndroid Build Coastguard Workernamespace base { 16*6777b538SAndroid Build Coastguard Worker 17*6777b538SAndroid Build Coastguard Worker// The following code would perform a linear search through the set which is 18*6777b538SAndroid Build Coastguard Worker// likely unexpected and not intended. This is because the expression 19*6777b538SAndroid Build Coastguard Worker// `set.find(kFoo)` is ill-formed, since there is no implimit conversion from 20*6777b538SAndroid Build Coastguard Worker// std::string_view to `std::string`. This means Contains would fall back to the 21*6777b538SAndroid Build Coastguard Worker// general purpose `base::ranges::find(set, kFoo)` linear search. 22*6777b538SAndroid Build Coastguard Worker// To fix this clients can either use a more generic comparator like std::less<> 23*6777b538SAndroid Build Coastguard Worker// (in this case `set.find()` accepts any type that is comparable to a 24*6777b538SAndroid Build Coastguard Worker// std::string), or pass an explicit projection parameter to Contains, at which 25*6777b538SAndroid Build Coastguard Worker// point it will always perform a linear search. 26*6777b538SAndroid Build Coastguard Workervoid UnexpectedLinearSearch() { 27*6777b538SAndroid Build Coastguard Worker constexpr std::string_view kFoo = "foo"; 28*6777b538SAndroid Build Coastguard Worker std::set<std::string> set = {"foo", "bar", "baz"}; 29*6777b538SAndroid Build Coastguard Worker Contains(set, kFoo); // expected-error@*:* {{About to perform linear search on an associative container}} 30*6777b538SAndroid Build Coastguard Worker} 31*6777b538SAndroid Build Coastguard Worker 32*6777b538SAndroid Build Coastguard Worker} // namespace base 33