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