Skip to content

Commit 93ffa98

Browse files
committed
Simplify null warnings to show where exactly we are suppressing nulls
1 parent f1e1f8b commit 93ffa98

File tree

6 files changed

+41
-33
lines changed

6 files changed

+41
-33
lines changed

dotnet/src/webdriver/Chromium/ChromiumDriver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ public DevToolsSession GetDevToolsSession(DevToolsOptions options)
330330

331331
try
332332
{
333-
DevToolsSession session = new DevToolsSession(debuggerAddress?.ToString(), options);
333+
DevToolsSession session = new DevToolsSession(debuggerAddress?.ToString()!, options);
334334
Task.Run(async () => await session.StartSession()).GetAwaiter().GetResult();
335335
this.devToolsSession = session;
336336
}

dotnet/src/webdriver/NetworkAuthenticationHandler.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// </copyright>
1919

2020
using System;
21+
using System.Diagnostics.CodeAnalysis;
2122

2223
#nullable enable
2324

@@ -32,12 +33,14 @@ public class NetworkAuthenticationHandler
3233
/// Gets or sets a function that takes a <see cref="Uri"/> object, and returns a
3334
/// value indicating whether the supplied URI matches the specified criteria.
3435
/// </summary>
35-
public Func<Uri, bool> UriMatcher { get; set; } = null!;
36+
[DisallowNull]
37+
public Func<Uri, bool>? UriMatcher { get; set; }
3638

3739
/// <summary>
3840
/// Gets or sets the credentials to use when responding to an authentication request
3941
/// that matches the specified criteria.
4042
/// </summary>
41-
public ICredentials Credentials { get; set; } = null!;
43+
[DisallowNull]
44+
public ICredentials? Credentials { get; set; }
4245
}
4346
}

dotnet/src/webdriver/NetworkManager.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,9 @@ private async Task OnAuthRequired(object sender, AuthRequiredEventArgs e)
199199
bool successfullyAuthenticated = false;
200200
foreach (var authenticationHandler in this.authenticationHandlers)
201201
{
202-
if (authenticationHandler.UriMatcher.Invoke(uri))
202+
if (authenticationHandler.UriMatcher!.Invoke(uri))
203203
{
204-
PasswordCredentials credentials = (PasswordCredentials)authenticationHandler.Credentials;
204+
PasswordCredentials credentials = (PasswordCredentials)authenticationHandler.Credentials!;
205205
await this.session.Value.Domains.Network.ContinueWithAuth(e.RequestId, credentials.UserName, credentials.Password).ConfigureAwait(false);
206206
successfullyAuthenticated = true;
207207
break;
@@ -223,7 +223,7 @@ private async Task OnRequestPaused(object sender, RequestPausedEventArgs e)
223223

224224
foreach (var handler in this.requestHandlers)
225225
{
226-
if (handler.RequestMatcher.Invoke(e.RequestData))
226+
if (handler.RequestMatcher!.Invoke(e.RequestData))
227227
{
228228
if (handler.RequestTransformer != null)
229229
{
@@ -257,14 +257,14 @@ private async Task OnResponsePaused(object sender, ResponsePausedEventArgs e)
257257

258258
foreach (var handler in this.responseHandlers)
259259
{
260-
if (handler.ResponseMatcher.Invoke(e.ResponseData))
260+
if (handler.ResponseMatcher!.Invoke(e.ResponseData))
261261
{
262262
// NOTE: We create a dummy HttpRequestData object here, because the ContinueRequestWithResponse
263263
// method demands one; however, the only property used by that method is the RequestId property.
264264
// It might be better to refactor that method signature to simply pass the request ID, or
265265
// alternatively, just pass the response data, which should also contain the request ID anyway.
266266
HttpRequestData requestData = new HttpRequestData { RequestId = e.ResponseData.RequestId };
267-
await this.session.Value.Domains.Network.ContinueRequestWithResponse(requestData, handler.ResponseTransformer(e.ResponseData)).ConfigureAwait(false);
267+
await this.session.Value.Domains.Network.ContinueRequestWithResponse(requestData, handler.ResponseTransformer!(e.ResponseData)).ConfigureAwait(false);
268268
return;
269269
}
270270
}

dotnet/src/webdriver/NetworkRequestHandler.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// </copyright>
1919

2020
using System;
21+
using System.Diagnostics.CodeAnalysis;
2122

2223
#nullable enable
2324

@@ -32,7 +33,8 @@ public class NetworkRequestHandler
3233
/// Gets or sets a function that evaluates request data in an <see cref="HttpRequestData"/> object,
3334
/// and returns a value indicating whether the data matches the specified criteria.
3435
/// </summary>
35-
public Func<HttpRequestData, bool> RequestMatcher { get; set; } = null!;
36+
[DisallowNull]
37+
public Func<HttpRequestData, bool>? RequestMatcher { get; set; }
3638

3739
/// <summary>
3840
/// Gets or sets a function that accepts an <see cref="HttpRequestData"/> object describing a network

dotnet/src/webdriver/NetworkResponseHandler.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// </copyright>
1919

2020
using System;
21+
using System.Diagnostics.CodeAnalysis;
2122

2223
#nullable enable
2324

@@ -32,13 +33,15 @@ public class NetworkResponseHandler
3233
/// Gets or sets a function that evaluates returned response data in an <see cref="HttpResponseData"/> object,
3334
/// and returns a value indicating whether the data matches the specified criteria.
3435
/// </summary>
35-
public Func<HttpResponseData, bool> ResponseMatcher { get; set; } = null!;
36+
[DisallowNull]
37+
public Func<HttpResponseData, bool>? ResponseMatcher { get; set; }
3638

3739
/// <summary>
3840
/// Gets or sets a function that accepts an <see cref="HttpResponseData"/> object describing a network
3941
/// response received by the browser, and returns a modified <see cref="HttpResponseData"/> object to used
4042
/// as the actual network response.
4143
/// </summary>
42-
public Func<HttpResponseData, HttpResponseData> ResponseTransformer { get; set; } = null!;
44+
[DisallowNull]
45+
public Func<HttpResponseData, HttpResponseData>? ResponseTransformer { get; set; }
4346
}
4447
}

dotnet/src/webdriver/RelativeBy.cs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ namespace OpenQA.Selenium
3333
/// </summary>
3434
public class RelativeBy : By
3535
{
36-
private string wrappedAtom;
37-
private object root;
38-
private List<object> filters = new List<object>();
36+
private readonly string wrappedAtom;
37+
private readonly object root;
38+
private readonly List<object> filters = new List<object>();
3939

4040
/// <summary>
4141
/// Prevents a default instance of the <see cref="RelativeBy"/> class.
4242
/// </summary>
4343
protected RelativeBy() : base()
4444
{
45-
string atom = string.Empty;
45+
string atom;
4646
using (Stream atomStream = ResourceUtilities.GetResourceStream("find-elements.js", "find-elements.js"))
4747
{
4848
using (StreamReader atomReader = new StreamReader(atomStream))
@@ -56,7 +56,7 @@ protected RelativeBy() : base()
5656

5757
private RelativeBy(object root, List<object>? filters = null) : this()
5858
{
59-
this.root = this.GetSerializableRoot(root);
59+
this.root = GetSerializableRoot(root);
6060

6161
if (filters != null)
6262
{
@@ -105,7 +105,7 @@ public override ReadOnlyCollection<IWebElement> FindElements(ISearchContext cont
105105
IJavaScriptExecutor js = GetExecutor(context);
106106
Dictionary<string, object> parameters = new Dictionary<string, object>();
107107
Dictionary<string, object> filterParameters = new Dictionary<string, object>();
108-
filterParameters["root"] = this.GetSerializableObject(this.root);
108+
filterParameters["root"] = GetSerializableObject(this.root);
109109
filterParameters["filters"] = this.filters;
110110
parameters["relative"] = filterParameters;
111111
object rawElements = js.ExecuteScript(wrappedAtom, parameters);
@@ -279,6 +279,7 @@ public RelativeBy Near(IWebElement element)
279279
/// <param name="atMostDistanceInPixels">The maximum distance from the element to be considered "near."</param>
280280
/// <returns>A <see cref="RelativeBy"/> object for use in finding the elements.</returns>
281281
/// <exception cref="ArgumentNullException">If <paramref name="element"/> is null.</exception>
282+
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="atMostDistanceInPixels"/> is not a positive value.</exception>
282283
public RelativeBy Near(IWebElement element, int atMostDistanceInPixels)
283284
{
284285
return Near((object)element, atMostDistanceInPixels);
@@ -302,6 +303,7 @@ public RelativeBy Near(By locator)
302303
/// <param name="atMostDistanceInPixels">The maximum distance from the element to be considered "near."</param>
303304
/// <returns>A <see cref="RelativeBy"/> object for use in finding the elements.</returns>
304305
/// <exception cref="ArgumentNullException">If <paramref name="locator"/> is null.</exception>
306+
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="atMostDistanceInPixels"/> is not a positive value.</exception>
305307
public RelativeBy Near(By locator, int atMostDistanceInPixels)
306308
{
307309
return Near((object)locator, atMostDistanceInPixels);
@@ -316,7 +318,7 @@ private RelativeBy Near(object locator, int atMostDistanceInPixels)
316318

317319
if (atMostDistanceInPixels <= 0)
318320
{
319-
throw new ArgumentException("Distance must be greater than zero", nameof(atMostDistanceInPixels));
321+
throw new ArgumentOutOfRangeException(nameof(atMostDistanceInPixels), "Distance must be greater than zero");
320322
}
321323

322324
Dictionary<string, object> filter = new Dictionary<string, object>();
@@ -347,61 +349,59 @@ private RelativeBy SimpleDirection(string direction, object locator)
347349
return new RelativeBy(this.root, this.filters);
348350
}
349351

350-
private object GetSerializableRoot(object toSerialize)
352+
private static object GetSerializableRoot(object root)
351353
{
352-
if (toSerialize == null)
354+
if (root == null)
353355
{
354-
throw new ArgumentNullException(nameof(toSerialize), "object to serialize must not be null");
356+
throw new ArgumentNullException(nameof(root), "object to serialize must not be null");
355357
}
356358

357-
By? asBy = toSerialize as By;
358-
if (asBy != null)
359+
if (root is By asBy)
359360
{
360361
return asBy;
361362
}
362363

363-
if (toSerialize is IWebElement element)
364+
if (root is IWebElement element)
364365
{
365366
return element;
366367
}
367368

368-
if (toSerialize is IWrapsElement wrapper)
369+
if (root is IWrapsElement wrapper)
369370
{
370371
return wrapper.WrappedElement;
371372
}
372373

373374
throw new WebDriverException("Serializable locator must be a By, an IWebElement, or a wrapped element using IWrapsElement");
374375
}
375376

376-
private object GetSerializableObject(object toSerialize)
377+
private static object GetSerializableObject(object root)
377378
{
378-
if (toSerialize == null)
379+
if (root == null)
379380
{
380-
throw new ArgumentNullException(nameof(toSerialize), "object to serialize must not be null");
381+
throw new ArgumentNullException(nameof(root), "object to serialize must not be null");
381382
}
382383

383-
By? asBy = toSerialize as By;
384-
if (asBy != null)
384+
if (root is By asBy)
385385
{
386386
Dictionary<string, object> serializedBy = new Dictionary<string, object>();
387387
serializedBy[asBy.Mechanism] = asBy.Criteria;
388388
return serializedBy;
389389
}
390390

391-
if (toSerialize is IWebElement element)
391+
if (root is IWebElement element)
392392
{
393393
return element;
394394
}
395395

396-
if (toSerialize is IWrapsElement wrapper)
396+
if (root is IWrapsElement wrapper)
397397
{
398398
return wrapper.WrappedElement;
399399
}
400400

401401
throw new WebDriverException("Serializable locator must be a By, an IWebElement, or a wrapped element using IWrapsElement");
402402
}
403403

404-
private IJavaScriptExecutor GetExecutor(ISearchContext context)
404+
private static IJavaScriptExecutor GetExecutor(ISearchContext context)
405405
{
406406
IJavaScriptExecutor? executor = context as IJavaScriptExecutor;
407407
if (executor != null)

0 commit comments

Comments
 (0)