IDE0016: use throw expression
diff --git a/src/Lucene.Net/Analysis/Tokenizer.cs b/src/Lucene.Net/Analysis/Tokenizer.cs
index f94a5bb..29fc581 100644
--- a/src/Lucene.Net/Analysis/Tokenizer.cs
+++ b/src/Lucene.Net/Analysis/Tokenizer.cs
@@ -53,11 +53,7 @@
protected internal Tokenizer(AttributeFactory factory, TextReader input)
: base(factory)
{
- if (input == null)
- {
- throw new ArgumentNullException(nameof(input), "input must not be null");
- }
- this.inputPending = input;
+ this.inputPending = input ?? throw new ArgumentNullException(nameof(input), "input must not be null");
}
/// <summary>
diff --git a/src/Lucene.Net/Document/Field.cs b/src/Lucene.Net/Document/Field.cs
index 25b298a..e1c015a 100644
--- a/src/Lucene.Net/Document/Field.cs
+++ b/src/Lucene.Net/Document/Field.cs
@@ -155,18 +155,10 @@
/// is <c>null</c>, or if the reader is <c>null</c> </exception>
public Field(string name, TextReader reader, FieldType type)
{
- if (name == null)
- {
- throw new ArgumentNullException(nameof(name), "name cannot be null");
- }
- if (type == null)
+ if (type is null)
{
throw new ArgumentNullException(nameof(type), "type cannot be null");
}
- if (reader == null)
- {
- throw new ArgumentNullException(nameof(reader), "reader cannot be null");
- }
if (type.IsStored)
{
throw new ArgumentException("fields with a Reader value cannot be stored");
@@ -176,8 +168,8 @@
throw new ArgumentException("non-tokenized fields must use String values");
}
- this.m_name = name;
- this.FieldsData = reader;
+ this.m_name = name ?? throw new ArgumentNullException(nameof(name), "name cannot be null");
+ this.FieldsData = reader ?? throw new ArgumentNullException(nameof(reader), "reader cannot be null");
this.m_type = type;
}
@@ -192,15 +184,7 @@
/// is <c>null</c>, or if the <paramref name="tokenStream"/> is <c>null</c> </exception>
public Field(string name, TokenStream tokenStream, FieldType type)
{
- if (name == null)
- {
- throw new ArgumentNullException(nameof(name), "name cannot be null");
- }
- if (tokenStream == null)
- {
- throw new ArgumentNullException(nameof(tokenStream), "tokenStream cannot be null");
- }
- if (type == null)
+ if (type is null)
{
throw new ArgumentNullException(nameof(type), "type cannot be null");
}
@@ -213,9 +197,9 @@
throw new ArgumentException("TokenStream fields cannot be stored");
}
- this.m_name = name;
+ this.m_name = name ?? throw new ArgumentNullException(nameof(name), "name cannot be null");
this.FieldsData = null;
- this.m_tokenStream = tokenStream;
+ this.m_tokenStream = tokenStream ?? throw new ArgumentNullException(nameof(tokenStream), "tokenStream cannot be null");
this.m_type = type;
}
@@ -269,21 +253,13 @@
/// or the <paramref name="type"/> is <c>null</c> </exception>
public Field(string name, BytesRef bytes, FieldType type)
{
- if (name == null)
- {
- throw new ArgumentNullException(nameof(name), "name cannot be null");
- }
- if (type == null)
- {
- throw new ArgumentNullException(nameof(type), "type cannot be null");
- }
+ // LUCENENET specific - rearranged order to take advantage of throw expressions
+ this.m_name = name ?? throw new ArgumentNullException(nameof(name), "name cannot be null");
+ this.m_type = type ?? throw new ArgumentNullException(nameof(type), "type cannot be null");
if (type.IsIndexed)
- {
throw new ArgumentException("Fields with BytesRef values cannot be indexed");
- }
+
this.FieldsData = bytes;
- this.m_type = type;
- this.m_name = name;
}
// TODO: allow direct construction of int, long, float, double value too..?
@@ -299,15 +275,7 @@
/// is <c>null</c>, or if the <paramref name="type"/> is <c>null</c> </exception>
public Field(string name, string value, FieldType type)
{
- if (name == null)
- {
- throw new ArgumentNullException(nameof(name), "name cannot be null");
- }
- if (value == null)
- {
- throw new ArgumentNullException(nameof(value), "value cannot be null");
- }
- if (type == null)
+ if (type is null)
{
throw new ArgumentNullException(nameof(type), "type cannot be null");
}
@@ -321,8 +289,8 @@
}
this.m_type = type;
- this.m_name = name;
- this.FieldsData = value;
+ this.m_name = name ?? throw new ArgumentNullException(nameof(name), "name cannot be null");
+ this.FieldsData = value ?? throw new ArgumentNullException(nameof(value), "value cannot be null");
}
/// <summary>
diff --git a/src/Lucene.Net/Index/IndexWriterConfig.cs b/src/Lucene.Net/Index/IndexWriterConfig.cs
index 105bf90..6f7b230 100644
--- a/src/Lucene.Net/Index/IndexWriterConfig.cs
+++ b/src/Lucene.Net/Index/IndexWriterConfig.cs
@@ -240,14 +240,7 @@
new public IndexDeletionPolicy IndexDeletionPolicy
{
get => delPolicy;
- set
- {
- if (value == null)
- {
- throw new ArgumentException("indexDeletionPolicy must not be null");
- }
- this.delPolicy = value;
- }
+ set => delPolicy = value ?? throw new ArgumentNullException(nameof(value), "IndexDeletionPolicy must not be null");
}
/// <summary>
@@ -276,14 +269,7 @@
new public Similarity Similarity
{
get => similarity;
- set
- {
- if (value == null)
- {
- throw new ArgumentException("similarity must not be null");
- }
- this.similarity = value;
- }
+ set => similarity = value ?? throw new ArgumentNullException(nameof(value), "Similarity must not be null");
}
@@ -310,18 +296,8 @@
// so must declare it new. See: http://stackoverflow.com/q/82437
new public IMergeScheduler MergeScheduler
{
- get
- {
- return mergeScheduler;
- }
- set
- {
- if (value == null)
- {
- throw new ArgumentException("mergeScheduler must not be null");
- }
- this.mergeScheduler = value;
- }
+ get => mergeScheduler;
+ set => mergeScheduler = value ?? throw new ArgumentNullException(nameof(value), "MergeScheduler must not be null");
}
/// <summary>
@@ -341,7 +317,6 @@
/// <summary>
/// Gets or sets the <see cref="Codecs.Codec"/>.
- ///
/// <para/>
/// Only takes effect when <see cref="IndexWriter"/> is first created.
/// </summary>
@@ -350,14 +325,7 @@
new public Codec Codec
{
get => codec;
- set
- {
- if (value == null)
- {
- throw new ArgumentException("codec must not be null");
- }
- this.codec = value;
- }
+ set => codec = value ?? throw new ArgumentException(nameof(value), "Codec must not be null");
}
/// <summary>
@@ -373,14 +341,7 @@
new public MergePolicy MergePolicy
{
get => mergePolicy;
- set
- {
- if (value == null)
- {
- throw new ArgumentException("mergePolicy must not be null");
- }
- this.mergePolicy = value;
- }
+ set => mergePolicy = value ?? throw new ArgumentNullException(nameof(value), "MergePolicy must not be null");
}
/// <summary>
@@ -403,14 +364,7 @@
new internal DocumentsWriterPerThreadPool IndexerThreadPool
{
get => indexerThreadPool;
- set
- {
- if (value == null)
- {
- throw new ArgumentException("threadPool must not be null");
- }
- this.indexerThreadPool = value;
- }
+ set => indexerThreadPool = value ?? throw new ArgumentNullException(nameof(value), "IndexerThreadPool must not be null");
}
/// <summary>
@@ -472,14 +426,7 @@
new internal IndexingChain IndexingChain
{
get => indexingChain;
- set
- {
- if (value == null)
- {
- throw new ArgumentException("indexingChain must not be null");
- }
- this.indexingChain = value;
- }
+ set => indexingChain = value ?? throw new ArgumentNullException(nameof(value), "IndexingChain must not be null");
}
/// <summary>
@@ -520,14 +467,7 @@
new internal FlushPolicy FlushPolicy
{
get => flushPolicy;
- set
- {
- if (value == null)
- {
- throw new ArgumentException("flushPolicy must not be null");
- }
- this.flushPolicy = value;
- }
+ set => flushPolicy = value ?? throw new ArgumentNullException(nameof(value), "FlushPolicy must not be null");
}
// LUCENENT NOTE: The following properties would be pointless,
@@ -604,12 +544,9 @@
/// </summary>
public IndexWriterConfig SetInfoStream(InfoStream infoStream)
{
- if (infoStream == null)
- {
- throw new ArgumentException("Cannot set InfoStream implementation to null. " +
+ this.infoStream = infoStream ?? throw new ArgumentNullException(nameof(infoStream),
+ "Cannot set InfoStream implementation to null. " +
"To disable logging use InfoStream.NO_OUTPUT");
- }
- this.infoStream = infoStream;
return this;
}
diff --git a/src/Lucene.Net/Index/SegmentInfo.cs b/src/Lucene.Net/Index/SegmentInfo.cs
index b96675e..3998629 100644
--- a/src/Lucene.Net/Index/SegmentInfo.cs
+++ b/src/Lucene.Net/Index/SegmentInfo.cs
@@ -137,12 +137,8 @@
get => codec;
set
{
- if (Debugging.AssertsEnabled) Debugging.Assert(this.codec == null);
- if (value == null)
- {
- throw new ArgumentException("codec must be non-null");
- }
- this.codec = value;
+ if (Debugging.AssertsEnabled) Debugging.Assert(this.codec is null);
+ this.codec = value ?? throw new ArgumentNullException(nameof(value), "Codec must be non-null");
}
}
diff --git a/src/Lucene.Net/Search/BitsFilteredDocIdSet.cs b/src/Lucene.Net/Search/BitsFilteredDocIdSet.cs
index b31790f..1b689ed 100644
--- a/src/Lucene.Net/Search/BitsFilteredDocIdSet.cs
+++ b/src/Lucene.Net/Search/BitsFilteredDocIdSet.cs
@@ -50,11 +50,7 @@
public BitsFilteredDocIdSet(DocIdSet innerSet, IBits acceptDocs)
: base(innerSet)
{
- if (acceptDocs == null)
- {
- throw new ArgumentNullException("acceptDocs is null");
- }
- this.acceptDocs = acceptDocs;
+ this.acceptDocs = acceptDocs ?? throw new ArgumentNullException(nameof(acceptDocs), "acceptDocs can not be null");
}
protected override bool Match(int docid)
diff --git a/src/Lucene.Net/Search/FilteredDocIdSetIterator.cs b/src/Lucene.Net/Search/FilteredDocIdSetIterator.cs
index c9d8c46..01162e3 100644
--- a/src/Lucene.Net/Search/FilteredDocIdSetIterator.cs
+++ b/src/Lucene.Net/Search/FilteredDocIdSetIterator.cs
@@ -35,11 +35,7 @@
/// <param name="innerIter"> Underlying <see cref="DocIdSetIterator"/>. </param>
public FilteredDocIdSetIterator(DocIdSetIterator innerIter)
{
- if (innerIter == null)
- {
- throw new ArgumentException("null iterator");
- }
- m_innerIter = innerIter;
+ m_innerIter = innerIter ?? throw new ArgumentNullException(nameof(innerIter), "null iterator");
doc = -1;
}
diff --git a/src/Lucene.Net/Search/FilteredQuery.cs b/src/Lucene.Net/Search/FilteredQuery.cs
index 226f18b..e876693 100644
--- a/src/Lucene.Net/Search/FilteredQuery.cs
+++ b/src/Lucene.Net/Search/FilteredQuery.cs
@@ -64,17 +64,10 @@
/// <seealso cref="FilterStrategy"/>
public FilteredQuery(Query query, Filter filter, FilterStrategy strategy)
{
- if (query == null || filter == null)
- {
- throw new ArgumentException("Query and filter cannot be null.");
- }
- if (strategy == null)
- {
- throw new ArgumentException("FilterStrategy can not be null");
- }
- this.strategy = strategy;
- this.query = query;
- this.filter = filter;
+ // LUCENENET specific - rearranged order to take advantage of throw expressions
+ this.query = query ?? throw new ArgumentNullException(nameof(query), "Query cannot be null.");
+ this.filter = filter ?? throw new ArgumentNullException(nameof(filter), "filter can not be null");
+ this.strategy = strategy ?? throw new ArgumentNullException(nameof(strategy), "FilterStrategy can not be null");
}
/// <summary>
diff --git a/src/Lucene.Net/Search/MultiTermQuery.cs b/src/Lucene.Net/Search/MultiTermQuery.cs
index 128b6f0..fdc891a 100644
--- a/src/Lucene.Net/Search/MultiTermQuery.cs
+++ b/src/Lucene.Net/Search/MultiTermQuery.cs
@@ -269,11 +269,7 @@
/// </summary>
public MultiTermQuery(string field)
{
- if (field == null)
- {
- throw new ArgumentException("field must not be null");
- }
- this.m_field = field;
+ this.m_field = field ?? throw new ArgumentNullException(nameof(field), "field must not be null");
}
/// <summary>
diff --git a/src/Lucene.Net/Search/QueryWrapperFilter.cs b/src/Lucene.Net/Search/QueryWrapperFilter.cs
index 95ebe61..f60915f 100644
--- a/src/Lucene.Net/Search/QueryWrapperFilter.cs
+++ b/src/Lucene.Net/Search/QueryWrapperFilter.cs
@@ -1,22 +1,23 @@
+using System;
+
namespace Lucene.Net.Search
{
- using System;
/*
-* Licensed to the Apache Software Foundation (ASF) under one or more
-* contributor license agreements. See the NOTICE file distributed with
-* this work for additional information regarding copyright ownership.
-* The ASF licenses this file to You under the Apache License, Version 2.0
-* (the "License"); you may not use this file except in compliance with
-* the License. You may obtain a copy of the License at
-*
-* http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*/
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
using AtomicReaderContext = Lucene.Net.Index.AtomicReaderContext;
using IBits = Lucene.Net.Util.IBits;
@@ -40,11 +41,7 @@
/// </summary>
public QueryWrapperFilter(Query query)
{
- if (query == null)
- {
- throw new NullReferenceException("Query may not be null");
- }
- this.query = query;
+ this.query = query ?? throw new ArgumentNullException(nameof(query), "Query may not be null");
}
/// <summary>
diff --git a/src/Lucene.Net/Store/CompoundFileWriter.cs b/src/Lucene.Net/Store/CompoundFileWriter.cs
index 29f549a..b095584 100644
--- a/src/Lucene.Net/Store/CompoundFileWriter.cs
+++ b/src/Lucene.Net/Store/CompoundFileWriter.cs
@@ -89,17 +89,10 @@
/// if <paramref name="dir"/> or <paramref name="name"/> is <c>null</c> </exception>
internal CompoundFileWriter(Directory dir, string name)
{
- if (dir == null)
- {
- throw new ArgumentNullException("directory cannot be null");
- }
- if (name == null)
- {
- throw new ArgumentNullException("name cannot be null");
- }
- directory = dir;
+ // LUCENENET specific - changed order to take advantage of throw expression
+ directory = dir ?? throw new ArgumentNullException(nameof(directory), $"{nameof(directory)} cannot be null");
+ dataFileName = name ?? throw new ArgumentNullException(nameof(name), $"{nameof(name)} cannot be null");
entryTableName = IndexFileNames.SegmentFileName(IndexFileNames.StripExtension(name), "", IndexFileNames.COMPOUND_FILE_ENTRIES_EXTENSION);
- dataFileName = name;
}
private IndexOutput GetOutput()
diff --git a/src/Lucene.Net/Store/IndexInput.cs b/src/Lucene.Net/Store/IndexInput.cs
index 6d8bf57..80133b0 100644
--- a/src/Lucene.Net/Store/IndexInput.cs
+++ b/src/Lucene.Net/Store/IndexInput.cs
@@ -48,11 +48,7 @@
/// </summary>
protected IndexInput(string resourceDescription)
{
- if (resourceDescription == null)
- {
- throw new ArgumentException("resourceDescription must not be null");
- }
- this.resourceDescription = resourceDescription;
+ this.resourceDescription = resourceDescription ?? throw new ArgumentNullException(nameof(resourceDescription), $"{nameof(resourceDescription)} must not be null");
}
/// <summary>
diff --git a/src/Lucene.Net/Support/ListExtensions.cs b/src/Lucene.Net/Support/ListExtensions.cs
index fb9ece0..70a374e 100644
--- a/src/Lucene.Net/Support/ListExtensions.cs
+++ b/src/Lucene.Net/Support/ListExtensions.cs
@@ -52,17 +52,14 @@
/// <param name="toIndex">The exclusive ending index.</param>
public SubList(IList<T> list, int fromIndex, int toIndex)
{
- if (fromIndex < 0)
- throw new ArgumentOutOfRangeException(nameof(fromIndex));
-
- if (toIndex > list.Count)
- throw new ArgumentOutOfRangeException(nameof(toIndex));
-
- if (toIndex < fromIndex)
- throw new ArgumentOutOfRangeException(nameof(toIndex));
-
if (list == null)
throw new ArgumentNullException(nameof(list));
+ if (fromIndex < 0)
+ throw new ArgumentOutOfRangeException(nameof(fromIndex));
+ if (toIndex > list.Count)
+ throw new ArgumentOutOfRangeException(nameof(toIndex));
+ if (toIndex < fromIndex)
+ throw new ArgumentOutOfRangeException(nameof(toIndex));
this.list = list;
this.fromIndex = fromIndex;
diff --git a/src/Lucene.Net/Util/CharsRef.cs b/src/Lucene.Net/Util/CharsRef.cs
index 722727d..7c76a91 100644
--- a/src/Lucene.Net/Util/CharsRef.cs
+++ b/src/Lucene.Net/Util/CharsRef.cs
@@ -54,14 +54,7 @@
public char[] Chars
{
get => chars;
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException("Chars cannot be null");
- }
- chars = value;
- }
+ set => chars = value ?? throw new ArgumentNullException(nameof(value), "Chars cannot be null");
}
private char[] chars;
diff --git a/src/Lucene.Net/Util/FieldCacheSanityChecker.cs b/src/Lucene.Net/Util/FieldCacheSanityChecker.cs
index 8295b6b..9198c96 100644
--- a/src/Lucene.Net/Util/FieldCacheSanityChecker.cs
+++ b/src/Lucene.Net/Util/FieldCacheSanityChecker.cs
@@ -372,17 +372,12 @@
public Insanity(InsanityType type, string msg, params FieldCache.CacheEntry[] entries)
{
- if (null == type)
- {
- throw new ArgumentException("Insanity requires non-null InsanityType");
- }
- if (null == entries || 0 == entries.Length)
- {
- throw new ArgumentException("Insanity requires non-null/non-empty CacheEntry[]");
- }
- this.type = type;
+ // LUCENENET specific - rearranged order to take advantage of throw expressions
+ this.type = type ?? throw new ArgumentNullException(nameof(type), "Insanity requires non-null InsanityType");
+ this.entries = entries ?? throw new ArgumentNullException(nameof(entries), "Insanity requires non-null CacheEntry[]");
+ if (0 == entries.Length)
+ throw new ArgumentException("Insanity requires non-empty CacheEntry[]");
this.msg = msg;
- this.entries = entries;
}
/// <summary>
diff --git a/src/Lucene.Net/Util/InfoStream.cs b/src/Lucene.Net/Util/InfoStream.cs
index db4d004..d29a684 100644
--- a/src/Lucene.Net/Util/InfoStream.cs
+++ b/src/Lucene.Net/Util/InfoStream.cs
@@ -80,11 +80,10 @@
{
lock (typeof(InfoStream))
{
- if (value == null)
- {
- throw new ArgumentException("Cannot set InfoStream default implementation to null. " + "To disable logging use InfoStream.NO_OUTPUT");
- }
- defaultInfoStream = value;
+ defaultInfoStream = value ?? throw new ArgumentNullException(
+ nameof(value),
+ "Cannot set InfoStream default implementation to null. " +
+ "To disable logging use InfoStream.NO_OUTPUT");
}
}
}
diff --git a/src/Lucene.Net/Util/IntsRef.cs b/src/Lucene.Net/Util/IntsRef.cs
index f5effcf..2041951 100644
--- a/src/Lucene.Net/Util/IntsRef.cs
+++ b/src/Lucene.Net/Util/IntsRef.cs
@@ -57,14 +57,7 @@
public int[] Int32s // LUCENENET TODO: API - change to indexer
{
get => ints;
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException("Ints should never be null");
- }
- ints = value;
- }
+ set => ints = value ?? throw new ArgumentNullException(nameof(value), "Ints should never be null");
}
private int[] ints;