From 351535d30832e19b9257a07e36f84c0cf1dbe65d Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Sat, 12 Mar 2022 23:59:40 +0000 Subject: [PATCH 1/7] Fix nullable warning The compiler was throwing a warning that it was possible for the constructer to return null in certain circumstances. This rearranges the function so that cannot happen. --- nanoFramework.CoreLibrary/System/SpanByte.cs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/nanoFramework.CoreLibrary/System/SpanByte.cs b/nanoFramework.CoreLibrary/System/SpanByte.cs index 9c248a4d..050233ab 100644 --- a/nanoFramework.CoreLibrary/System/SpanByte.cs +++ b/nanoFramework.CoreLibrary/System/SpanByte.cs @@ -49,18 +49,21 @@ public SpanByte(byte[] array, int start, int length) { throw new ArgumentOutOfRangeException($"Array length too small"); } + else + { + _array = array; + _start = start; + _length = length; + } + } + else if ((start != 0) || (length != 0)) + { + throw new ArgumentOutOfRangeException($"Array is null but start and length are not 0"); } else { - if ((start != 0) || (length != 0)) - { - throw new ArgumentOutOfRangeException($"Array is null but start and length are not 0"); - } + throw new Exception("Could not generate SpanByte"); } - - _array = array; - _start = start; - _length = length; } /// From 3df3cd44d6359dafe43c7b6977dd144a4c092d8b Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Sun, 13 Mar 2022 00:31:32 +0000 Subject: [PATCH 2/7] Fix CodeSmell: Use NullReferenceException --- nanoFramework.CoreLibrary/System/SpanByte.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nanoFramework.CoreLibrary/System/SpanByte.cs b/nanoFramework.CoreLibrary/System/SpanByte.cs index 050233ab..136c5a01 100644 --- a/nanoFramework.CoreLibrary/System/SpanByte.cs +++ b/nanoFramework.CoreLibrary/System/SpanByte.cs @@ -62,7 +62,7 @@ public SpanByte(byte[] array, int start, int length) } else { - throw new Exception("Could not generate SpanByte"); + throw new NullReferenceException("Array is null"); } } From 609e4dc8c21834850874f7154677467b89c137b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Sun, 13 Mar 2022 11:37:22 +0000 Subject: [PATCH 3/7] Fix comments --- nanoFramework.CoreLibrary/System/SpanByte.cs | 30 ++++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/nanoFramework.CoreLibrary/System/SpanByte.cs b/nanoFramework.CoreLibrary/System/SpanByte.cs index 136c5a01..39f59eb3 100644 --- a/nanoFramework.CoreLibrary/System/SpanByte.cs +++ b/nanoFramework.CoreLibrary/System/SpanByte.cs @@ -35,8 +35,17 @@ public SpanByte(byte[] array) /// The index of the first element to include in the new System.Span /// The number of elements to include in the new System.Span /// - /// array is null, but start or length is non-zero. -or- start is outside the bounds - /// of the array. -or- start and length exceeds the number of elements in the array. + /// + /// array is null, but start or length is non-zero + /// + /// -or- + /// + /// start is outside the bounds of the array. + /// + /// -or- + /// + /// + exceed the number of elements in the array. + /// /// public SpanByte(byte[] array, int start, int length) { @@ -71,6 +80,9 @@ public SpanByte(byte[] array, int start, int length) /// /// The zero-based index of the element. /// The element at the specified index. + /// + /// is out of range. + /// public byte this[int index] { get @@ -114,7 +126,7 @@ public byte this[int index] /// /// The destination System.Span object. /// - /// destination is shorter than the source System.Span. + /// destination is shorter than the source . /// public void CopyTo(SpanByte destination) { @@ -130,23 +142,23 @@ public void CopyTo(SpanByte destination) } /// - /// Forms a slice out of the current span that begins at a specified index. + /// Forms a slice out of the current that begins at a specified index. /// /// The index at which to begin the slice. /// A span that consists of all elements of the current span from start to the end of the span. - /// start is less than zero or greater than System.Span.Length. + /// is < zero or > . public SpanByte Slice(int start) { return Slice(start, _length - start); } /// - /// Forms a slice out of the current span starting at a specified index for a specified length. + /// Forms a slice out of the current starting at a specified index for a specified length. /// /// The index at which to begin this slice. /// The desired length for the slice. /// A span that consists of length elements from the current span starting at start. - /// start or start + length is less than zero or greater than System.Span.Length. + /// or + is < zero or > . public SpanByte Slice(int start, int length) { if ((start < 0) || (length < 0) || (start + length > _length)) @@ -158,7 +170,7 @@ public SpanByte Slice(int start, int length) } /// - /// Copies the contents of this span into a new array. + /// Copies the contents of this into a new array. /// /// An array containing the data in the current span. public byte[] ToArray() @@ -173,7 +185,7 @@ public byte[] ToArray() } /// - /// Implicit conversion of an array to a span of byte + /// Implicit conversion of an array to a . /// /// public static implicit operator SpanByte(byte[] array) From 95dc37c2d29470bbad0b873e425a3516461ec70c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Sun, 13 Mar 2022 11:49:53 +0000 Subject: [PATCH 4/7] Remove messages from exception constructors - Also add compiler defs to disable sonarcloud analysis on exceptions. --- nanoFramework.CoreLibrary/System/SpanByte.cs | 28 +++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/nanoFramework.CoreLibrary/System/SpanByte.cs b/nanoFramework.CoreLibrary/System/SpanByte.cs index 39f59eb3..be25abea 100644 --- a/nanoFramework.CoreLibrary/System/SpanByte.cs +++ b/nanoFramework.CoreLibrary/System/SpanByte.cs @@ -56,7 +56,9 @@ public SpanByte(byte[] array, int start, int length) start + length > array.Length || (start == array.Length && start > 0)) { - throw new ArgumentOutOfRangeException($"Array length too small"); +#pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one + throw new ArgumentOutOfRangeException(); +#pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one } else { @@ -67,11 +69,15 @@ public SpanByte(byte[] array, int start, int length) } else if ((start != 0) || (length != 0)) { - throw new ArgumentOutOfRangeException($"Array is null but start and length are not 0"); +#pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one + throw new ArgumentOutOfRangeException(); +#pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one } else { - throw new NullReferenceException("Array is null"); +#pragma warning disable S3928 + throw new NullReferenceException(); +#pragma warning restore S3928 } } @@ -89,7 +95,9 @@ public byte this[int index] { if (index >= _length) { - throw new ArgumentOutOfRangeException($"Index out of range"); +#pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one + throw new ArgumentOutOfRangeException(); +#pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one } return _array[_start + index]; @@ -98,7 +106,9 @@ public byte this[int index] { if (index >= _length) { - throw new ArgumentOutOfRangeException($"Index out of range"); +#pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one + throw new ArgumentOutOfRangeException(); +#pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one } _array[_start + index] = value; @@ -132,7 +142,9 @@ public void CopyTo(SpanByte destination) { if (destination.Length < _length) { - throw new ArgumentException($"Destination too small"); +#pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one + throw new ArgumentException(); +#pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one } for (int i = 0; i < _length; i++) @@ -163,7 +175,9 @@ public SpanByte Slice(int start, int length) { if ((start < 0) || (length < 0) || (start + length > _length)) { - throw new ArgumentOutOfRangeException($"start or start + length is less than zero or greater than length"); +#pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one + throw new ArgumentOutOfRangeException(); +#pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one } return new SpanByte(_array, _start + start, length); From 34f96f476a386eb43f115344af1a9f1a8662567a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Sun, 13 Mar 2022 12:00:53 +0000 Subject: [PATCH 5/7] Fix param check on constructor --- nanoFramework.CoreLibrary/System/SpanByte.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nanoFramework.CoreLibrary/System/SpanByte.cs b/nanoFramework.CoreLibrary/System/SpanByte.cs index be25abea..1708584e 100644 --- a/nanoFramework.CoreLibrary/System/SpanByte.cs +++ b/nanoFramework.CoreLibrary/System/SpanByte.cs @@ -54,7 +54,7 @@ public SpanByte(byte[] array, int start, int length) if (start < 0 || length < 0 || start + length > array.Length || - (start == array.Length && start > 0)) + start >= array.Length) { #pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one throw new ArgumentOutOfRangeException(); From 9050616c4309f96da55dce553a996988484ba41e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Sun, 13 Mar 2022 12:06:34 +0000 Subject: [PATCH 6/7] Fix comment close tags --- nanoFramework.CoreLibrary/System/SpanByte.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/nanoFramework.CoreLibrary/System/SpanByte.cs b/nanoFramework.CoreLibrary/System/SpanByte.cs index 1708584e..43b6042e 100644 --- a/nanoFramework.CoreLibrary/System/SpanByte.cs +++ b/nanoFramework.CoreLibrary/System/SpanByte.cs @@ -44,7 +44,7 @@ public SpanByte(byte[] array) /// /// -or- /// - /// + exceed the number of elements in the array. + /// + exceed the number of elements in the array. /// /// public SpanByte(byte[] array, int start, int length) @@ -87,7 +87,7 @@ public SpanByte(byte[] array, int start, int length) /// The zero-based index of the element. /// The element at the specified index. /// - /// is out of range. + /// is out of range. /// public byte this[int index] { @@ -158,7 +158,7 @@ public void CopyTo(SpanByte destination) /// /// The index at which to begin the slice. /// A span that consists of all elements of the current span from start to the end of the span. - /// is < zero or > . + /// is < zero or > . public SpanByte Slice(int start) { return Slice(start, _length - start); @@ -170,7 +170,7 @@ public SpanByte Slice(int start) /// The index at which to begin this slice. /// The desired length for the slice. /// A span that consists of length elements from the current span starting at start. - /// or + is < zero or > . + /// or + is < zero or > . public SpanByte Slice(int start, int length) { if ((start < 0) || (length < 0) || (start + length > _length)) From 3c0595d74687e73fd5526c638f6f0266eab60340 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Sun, 13 Mar 2022 12:22:47 +0000 Subject: [PATCH 7/7] More fixes in comments tags --- nanoFramework.CoreLibrary/System/SpanByte.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/nanoFramework.CoreLibrary/System/SpanByte.cs b/nanoFramework.CoreLibrary/System/SpanByte.cs index 43b6042e..b9d60a8d 100644 --- a/nanoFramework.CoreLibrary/System/SpanByte.cs +++ b/nanoFramework.CoreLibrary/System/SpanByte.cs @@ -44,7 +44,7 @@ public SpanByte(byte[] array) /// /// -or- /// - /// + exceed the number of elements in the array. + /// + exceed the number of elements in the array. /// /// public SpanByte(byte[] array, int start, int length) @@ -87,7 +87,7 @@ public SpanByte(byte[] array, int start, int length) /// The zero-based index of the element. /// The element at the specified index. /// - /// is out of range. + /// is out of range. /// public byte this[int index] { @@ -158,7 +158,7 @@ public void CopyTo(SpanByte destination) /// /// The index at which to begin the slice. /// A span that consists of all elements of the current span from start to the end of the span. - /// is < zero or > . + /// is < zero or > . public SpanByte Slice(int start) { return Slice(start, _length - start); @@ -169,8 +169,8 @@ public SpanByte Slice(int start) /// /// The index at which to begin this slice. /// The desired length for the slice. - /// A span that consists of length elements from the current span starting at start. - /// or + is < zero or > . + /// A that consists of number of elements from the current starting at . + /// or + is < zero or > . public SpanByte Slice(int start, int length) { if ((start < 0) || (length < 0) || (start + length > _length))