You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucenenet.apache.org by "paulirwin (via GitHub)" <gi...@apache.org> on 2024/11/21 05:21:46 UTC
[PR] Fix csharpsquid:S1117 SonarCloud warnings about shadowing fields with variable names [lucenenet]
paulirwin opened a new pull request, #1042:
URL: https://github.com/apache/lucenenet/pull/1042
- [X] You've read the [Contributor Guide](https://github.com/apache/lucenenet/blob/main/CONTRIBUTING.md) and [Code of Conduct](https://www.apache.org/foundation/policies/conduct.html).
- [ ] You've included unit or integration tests for your change, where applicable.
- [X] You've included inline docs for your change, where applicable.
- [X] There's an open issue for the PR that you are making. If you'd like to propose a change, please [open an issue](https://github.com/apache/lucenenet/issues/new/choose) to discuss the change or find an existing issue.
Fix csharpsquid:S1117 SonarCloud warnings about shadowing fields with variable names
Fixes #678
## Description
This renames variables that shadowed fields of the same name, causing the SonarCloud S1117 warning. In the case of methods that replace static constructor initialization, this was named `result` instead of the field name. In other cases where there was no Lucene equivalent, the variable was renamed.
This also fixes a bug in FieldInfos where there was the possibility of a KeyNotFoundException due to a coding oversight.
Perhaps most importantly, this investigation discovered a potentially severe bug in `SortedSetDocValuesWriter.Flush`. Due to GetOrdCountEnumerable and GetOrdsEnumerable (previously named with a typo) having the same return type, they were accidentally swapped in their argument order to AddSortedSetField. I'm not quite sure what the impact would be, but it certainly was wrong. I fixed the typo in the names and fixed the order of their usage to match upstream. I also confirmed that the signature of AddSortedSetField expected them in this order.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix csharpsquid:S1117 SonarCloud warnings about shadowing fields with variable names [lucenenet]
Posted by "paulirwin (via GitHub)" <gi...@apache.org>.
paulirwin commented on PR #1042:
URL: https://github.com/apache/lucenenet/pull/1042#issuecomment-2490125364
After reviewing the changes again, I don't think the SortedSet code was actually buggy, the methods were just misnamed which caused confusion. Having the correct names will help avoid confusion in the future.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix csharpsquid:S1117 SonarCloud warnings about shadowing fields with variable names [lucenenet]
Posted by "NightOwl888 (via GitHub)" <gi...@apache.org>.
NightOwl888 commented on code in PR #1042:
URL: https://github.com/apache/lucenenet/pull/1042#discussion_r1851990264
##########
src/Lucene.Net/Support/CRC32.cs:
##########
@@ -29,7 +27,7 @@ internal class CRC32 : IChecksum
private static uint[] InitializeCRCTable()
Review Comment:
Given that the convention we typically follow for these is to use a `Load` prefix, let's do that here instead of `Initialize`. Also, add the comment
```c#
// LUCENENET: Avoid static constructors (see https://github.com/apache/lucenenet/pull/224#issuecomment-469284006)
```
##########
src/Lucene.Net/Index/SortedSetDocValuesWriter.cs:
##########
@@ -199,26 +201,26 @@ private IEnumerable<BytesRef> GetBytesRefEnumberable(int valueCount, int[] sorte
}
}
- private IEnumerable<long?> GetOrdsEnumberable(int maxDoc)
+ private IEnumerable<long?> GetOrdCountEnumerable(int maxDoc)
{
- AppendingDeltaPackedInt64Buffer.Iterator iter = pendingCounts.GetIterator();
+ var iter = pendingCounts.GetIterator();
- if (Debugging.AssertsEnabled) Debugging.Assert(pendingCounts.Count == maxDoc,"MaxDoc: {0}, pending.Count: {1}", maxDoc, pending.Count);
+ if (Debugging.AssertsEnabled) Debugging.Assert(pendingCounts.Count == maxDoc, "MaxDoc: {0}, pending.Count: {1}", maxDoc, pending.Count);
- for (int i = 0; i < maxDoc; ++i)
+ for (int docUpto = 0; docUpto < maxDoc; ++docUpto)
{
yield return iter.Next();
}
}
- private IEnumerable<long?> GetOrdCountEnumberable(int maxCountPerDoc, int[] ordMap)
+ private IEnumerable<long?> GetOrdsEnumerable(int[] ordMap, int maxCountPerDoc)
{
int currentUpTo = 0, currentLength = 0;
- AppendingPackedInt64Buffer.Iterator iter = pending.GetIterator();
- AppendingDeltaPackedInt64Buffer.Iterator counts = pendingCounts.GetIterator();
- int[] currentDoc = new int[maxCountPerDoc];
+ var iter = pending.GetIterator();
+ var counts = pendingCounts.GetIterator();
Review Comment:
`var` only makes sense if it is clear from the line what type the variable is. In this case, it is not because `GetIterator()` tells us nothing of the type. Keep in mind, we may be reviewing this on GitHub without having an IDE handy to tell what type `var` resolves to so having the type name visible in the source code could be helpful.
##########
src/Lucene.Net/Index/SortedSetDocValuesWriter.cs:
##########
@@ -175,21 +175,23 @@ public override void Flush(SegmentWriteState state, DocValuesConsumer dvConsumer
ordMap[sortedValues[ord]] = ord;
}
- dvConsumer.AddSortedSetField(fieldInfo, GetBytesRefEnumberable(valueCount, sortedValues),
+ dvConsumer.AddSortedSetField(fieldInfo,
+ // ord -> value
+ GetValuesEnumerable(valueCount, sortedValues),
- // doc -> ordCount
- GetOrdsEnumberable(maxDoc),
Review Comment:
I agree, these method names are easier to understand.
##########
src/Lucene.Net/Index/SortedSetDocValuesWriter.cs:
##########
@@ -199,26 +201,26 @@ private IEnumerable<BytesRef> GetBytesRefEnumberable(int valueCount, int[] sorte
}
}
- private IEnumerable<long?> GetOrdsEnumberable(int maxDoc)
+ private IEnumerable<long?> GetOrdCountEnumerable(int maxDoc)
{
- AppendingDeltaPackedInt64Buffer.Iterator iter = pendingCounts.GetIterator();
+ var iter = pendingCounts.GetIterator();
Review Comment:
`var` only makes sense if it is clear from the line what type the variable is. In this case, it is not because `GetIterator()` tells us nothing of the type. Keep in mind, we may be reviewing this on GitHub without having an IDE handy to tell what type `var` resolves to so having the type name visible in the source code could be helpful.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix csharpsquid:S1117 SonarCloud warnings about shadowing fields with variable names [lucenenet]
Posted by "paulirwin (via GitHub)" <gi...@apache.org>.
paulirwin merged PR #1042:
URL: https://github.com/apache/lucenenet/pull/1042
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix csharpsquid:S1117 SonarCloud warnings about shadowing fields with variable names [lucenenet]
Posted by "paulirwin (via GitHub)" <gi...@apache.org>.
paulirwin commented on code in PR #1042:
URL: https://github.com/apache/lucenenet/pull/1042#discussion_r1852240284
##########
src/Lucene.Net/Index/SortedSetDocValuesWriter.cs:
##########
@@ -199,26 +201,26 @@ private IEnumerable<BytesRef> GetBytesRefEnumberable(int valueCount, int[] sorte
}
}
- private IEnumerable<long?> GetOrdsEnumberable(int maxDoc)
+ private IEnumerable<long?> GetOrdCountEnumerable(int maxDoc)
{
- AppendingDeltaPackedInt64Buffer.Iterator iter = pendingCounts.GetIterator();
+ var iter = pendingCounts.GetIterator();
- if (Debugging.AssertsEnabled) Debugging.Assert(pendingCounts.Count == maxDoc,"MaxDoc: {0}, pending.Count: {1}", maxDoc, pending.Count);
+ if (Debugging.AssertsEnabled) Debugging.Assert(pendingCounts.Count == maxDoc, "MaxDoc: {0}, pending.Count: {1}", maxDoc, pending.Count);
- for (int i = 0; i < maxDoc; ++i)
+ for (int docUpto = 0; docUpto < maxDoc; ++docUpto)
{
yield return iter.Next();
}
}
- private IEnumerable<long?> GetOrdCountEnumberable(int maxCountPerDoc, int[] ordMap)
+ private IEnumerable<long?> GetOrdsEnumerable(int[] ordMap, int maxCountPerDoc)
{
int currentUpTo = 0, currentLength = 0;
- AppendingPackedInt64Buffer.Iterator iter = pending.GetIterator();
- AppendingDeltaPackedInt64Buffer.Iterator counts = pendingCounts.GetIterator();
- int[] currentDoc = new int[maxCountPerDoc];
+ var iter = pending.GetIterator();
+ var counts = pendingCounts.GetIterator();
Review Comment:
Alright I'll revert these, I changed this because Rider was throwing this warning: https://www.jetbrains.com/help/rider/AccessToStaticMemberViaDerivedType.html
I'll suppress it in code instead.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org