Update Abseil instructions for absl::optional

Also add a presubmit check to verify we're not reintroducing it.

Bug: webrtc:342905193
Change-Id: Ic7eedb6a7fb257e3fd110b84d3921feb58f799d7
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/361282
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Florent Castelli <orphis@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#42912}
diff --git a/PRESUBMIT.py b/PRESUBMIT.py
index bd3bbc3..303b159 100755
--- a/PRESUBMIT.py
+++ b/PRESUBMIT.py
@@ -1047,6 +1047,9 @@
         CheckBannedAbslMakeUnique(input_api, output_api,
                                   non_third_party_sources))
     results.extend(
+        CheckBannedAbslOptional(input_api, output_api,
+                                non_third_party_sources))
+    results.extend(
         CheckObjcApiSymbols(input_api, output_api, non_third_party_sources))
     return results
 
@@ -1128,6 +1131,32 @@
     return []
 
 
+def CheckBannedAbslOptional(input_api, output_api, source_file_filter):
+    absl_optional = re.compile(r'absl::(optional|make_optional|nullopt)',
+                               re.MULTILINE)
+    absl_optional_include = re.compile(r'^#include\s*"absl/types/optional\.h"',
+                                       input_api.re.MULTILINE)
+    file_filter = lambda f: (f.LocalPath().endswith(
+        ('.cc', '.h')) and source_file_filter(f))
+
+    files = []
+    for f in input_api.AffectedFiles(include_deletes=False,
+                                     file_filter=file_filter):
+        for _, line in f.ChangedContents():
+            if absl_optional.search(line) or absl_optional_include.search(
+                    line):
+                files.append(f.LocalPath())
+                break
+
+    if files:
+        return [
+            output_api.PresubmitError(
+                'Please use std::optional instead of absl::optional.\n'
+                'Affected files:', files)
+        ]
+    return []
+
+
 def CheckObjcApiSymbols(input_api, output_api, source_file_filter):
     rtc_objc_export = re.compile(r'RTC_OBJC_EXPORT(.|\n){26}',
                                  re.MULTILINE | re.DOTALL)
diff --git a/g3doc/abseil-in-webrtc.md b/g3doc/abseil-in-webrtc.md
index a5803ff..f6c7f2e 100644
--- a/g3doc/abseil-in-webrtc.md
+++ b/g3doc/abseil-in-webrtc.md
@@ -32,7 +32,6 @@
 * `absl::InlinedVector`
 * `absl::Nonnull` and `absl::Nullable`
 * `absl::WrapUnique`
-* `absl::optional` and related stuff from `absl/types/optional.h`.
 * `absl::string_view`
 * The functions in `absl/strings/ascii.h`, `absl/strings/match.h`,
   and `absl/strings/str_replace.h`.
@@ -65,6 +64,10 @@
 spinlock initializer). Additionally, `absl::Mutex` handles time in a
 way that may not be compatible with the rest of WebRTC.
 
+### `absl::optional`
+
+*Use `std::optional` instead.*
+
 ### `absl::Span`
 
 *Use `rtc::ArrayView` instead.*