From e351e56edcfb485751c3ba88b8489bc8d3c554b2 Mon Sep 17 00:00:00 2001 From: marodev Date: Fri, 14 May 2021 23:27:12 +0200 Subject: [PATCH 1/5] Fix SonarQube's "Duplicate casts should not be made" / Code Smell --- .../src/OSS/Antlr/Atn/LL1Analyzer.cs | 3 +-- .../src/OSS/Antlr/Atn/LexerActionExecutor.cs | 4 ++-- .../src/OSS/Antlr/Atn/ParserATNSimulator.cs | 6 ++---- .../src/OSS/Antlr/BufferedTokenStream.cs | 4 ++-- .../src/OSS/Antlr/FailedPredicateException.cs | 4 ++-- .../src/OSS/Antlr/Misc/IntervalSet.cs | 3 +-- Microsoft.Azure.Cosmos/src/OSS/Antlr/Parser.cs | 3 +-- .../src/OSS/Antlr/ParserRuleContext.cs | 6 ++---- .../Antlr/Tree/Pattern/ParseTreePatternMatcher.cs | 9 +++------ Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Trees.cs | 12 ++++++------ .../src/OSS/Antlr/Tree/Xpath/XPathRuleElement.cs | 3 +-- .../src/OSS/Antlr/Tree/Xpath/XPathTokenElement.cs | 3 +-- .../src/OSS/Antlr/UnbufferedTokenStream.cs | 4 ++-- .../src/ResourceThrottleRetryPolicy.cs | 3 +-- 14 files changed, 27 insertions(+), 40 deletions(-) diff --git a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Atn/LL1Analyzer.cs b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Atn/LL1Analyzer.cs index 6893ab6eb7..db85b3d6ce 100644 --- a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Atn/LL1Analyzer.cs +++ b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Atn/LL1Analyzer.cs @@ -314,9 +314,8 @@ protected internal virtual void Look(ATNState s, ATNState stopState, PredictionC for (int i_1 = 0; i_1 < n; i_1++) { Transition t = s.Transition(i_1); - if (t is RuleTransition) + if (t is RuleTransition ruleTransition) { - RuleTransition ruleTransition = (RuleTransition)t; if (calledRuleStack.Get(ruleTransition.ruleIndex)) { continue; diff --git a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Atn/LexerActionExecutor.cs b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Atn/LexerActionExecutor.cs index b485522361..2430c7e434 100644 --- a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Atn/LexerActionExecutor.cs +++ b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Atn/LexerActionExecutor.cs @@ -223,9 +223,9 @@ public virtual void Execute(Lexer lexer, ICharStream input, int startIndex) foreach (ILexerAction lexerAction in lexerActions) { ILexerAction action = lexerAction; - if (action is LexerIndexedCustomAction) + if (action is LexerIndexedCustomAction lexerIndexedCustomAction) { - int offset = ((LexerIndexedCustomAction)action).Offset; + int offset = lexerIndexedCustomAction.Offset; input.Seek(startIndex + offset); action = ((LexerIndexedCustomAction)action).Action; requiresSeek = (startIndex + offset) != stopIndex; diff --git a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Atn/ParserATNSimulator.cs b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Atn/ParserATNSimulator.cs index 59eabbffb6..e5f46c6927 100644 --- a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Atn/ParserATNSimulator.cs +++ b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Atn/ParserATNSimulator.cs @@ -2124,14 +2124,12 @@ public void DumpDeadEndConfigs(NoViableAltException nvae) if (c.state.NumberOfTransitions > 0) { Transition t = c.state.Transition(0); - if (t is AtomTransition) + if (t is AtomTransition at) { - AtomTransition at = (AtomTransition)t; trans = "Atom " + GetTokenName(at.token); } - else if (t is SetTransition) + else if (t is SetTransition st) { - SetTransition st = (SetTransition)t; bool not = st is NotSetTransition; trans = (not ? "~" : "") + "Set " + st.set.ToString(); } diff --git a/Microsoft.Azure.Cosmos/src/OSS/Antlr/BufferedTokenStream.cs b/Microsoft.Azure.Cosmos/src/OSS/Antlr/BufferedTokenStream.cs index a3215b3d49..a63e85a5d5 100644 --- a/Microsoft.Azure.Cosmos/src/OSS/Antlr/BufferedTokenStream.cs +++ b/Microsoft.Azure.Cosmos/src/OSS/Antlr/BufferedTokenStream.cs @@ -241,9 +241,9 @@ protected internal virtual int Fetch(int n) for (int i = 0; i < n; i++) { IToken t = _tokenSource.NextToken(); - if (t is IWritableToken) + if (t is IWritableToken iWritableToken) { - ((IWritableToken)t).TokenIndex = tokens.Count; + iWritableToken.TokenIndex = tokens.Count; } tokens.Add(t); if (t.Type == TokenConstants.EOF) diff --git a/Microsoft.Azure.Cosmos/src/OSS/Antlr/FailedPredicateException.cs b/Microsoft.Azure.Cosmos/src/OSS/Antlr/FailedPredicateException.cs index f9c819766e..2dc71c689f 100644 --- a/Microsoft.Azure.Cosmos/src/OSS/Antlr/FailedPredicateException.cs +++ b/Microsoft.Azure.Cosmos/src/OSS/Antlr/FailedPredicateException.cs @@ -46,9 +46,9 @@ public FailedPredicateException(Parser recognizer, string predicate, string mess { ATNState s = recognizer.Interpreter.atn.states[recognizer.State]; AbstractPredicateTransition trans = (AbstractPredicateTransition)s.Transition(0); - if (trans is PredicateTransition) + if (trans is PredicateTransition predicateTransition) { - this.ruleIndex = ((PredicateTransition)trans).ruleIndex; + this.ruleIndex = predicateTransition.ruleIndex; this.predicateIndex = ((PredicateTransition)trans).predIndex; } else diff --git a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Misc/IntervalSet.cs b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Misc/IntervalSet.cs index dd520a27ec..08929c918e 100644 --- a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Misc/IntervalSet.cs +++ b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Misc/IntervalSet.cs @@ -206,9 +206,8 @@ public virtual Antlr4.Runtime.Misc.IntervalSet AddAll(IIntSet set) { return this; } - if (set is Antlr4.Runtime.Misc.IntervalSet) + if (set is Antlr4.Runtime.Misc.IntervalSet other) { - Antlr4.Runtime.Misc.IntervalSet other = (Antlr4.Runtime.Misc.IntervalSet)set; // walk set and add each interval int n = other.intervals.Count; for (int i = 0; i < n; i++) diff --git a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Parser.cs b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Parser.cs index 7fafedfbc6..fe3519f204 100644 --- a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Parser.cs +++ b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Parser.cs @@ -604,9 +604,8 @@ public virtual ParseTreePattern CompileParseTreePattern(string pattern, int patt if (((ITokenStream)InputStream) != null) { ITokenSource tokenSource = ((ITokenStream)InputStream).TokenSource; - if (tokenSource is Lexer) + if (tokenSource is Lexer lexer) { - Lexer lexer = (Lexer)tokenSource; return CompileParseTreePattern(pattern, patternRuleIndex, lexer); } } diff --git a/Microsoft.Azure.Cosmos/src/OSS/Antlr/ParserRuleContext.cs b/Microsoft.Azure.Cosmos/src/OSS/Antlr/ParserRuleContext.cs index 5606464acf..ae62075c09 100644 --- a/Microsoft.Azure.Cosmos/src/OSS/Antlr/ParserRuleContext.cs +++ b/Microsoft.Azure.Cosmos/src/OSS/Antlr/ParserRuleContext.cs @@ -265,9 +265,8 @@ public virtual ITerminalNode GetToken(int ttype, int i) // what token with ttype have we found? foreach (IParseTree o in children) { - if (o is ITerminalNode) + if (o is ITerminalNode tnode) { - ITerminalNode tnode = (ITerminalNode)o; IToken symbol = tnode.Symbol; if (symbol.Type == ttype) { @@ -295,9 +294,8 @@ public virtual ITerminalNode[] GetTokens(int ttype) List tokens = null; foreach (IParseTree o in children) { - if (o is ITerminalNode) + if (o is ITerminalNode tnode) { - ITerminalNode tnode = (ITerminalNode)o; IToken symbol = tnode.Symbol; if (symbol.Type == ttype) { diff --git a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Pattern/ParseTreePatternMatcher.cs b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Pattern/ParseTreePatternMatcher.cs index 591a991721..a3c9c6e07e 100644 --- a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Pattern/ParseTreePatternMatcher.cs +++ b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Pattern/ParseTreePatternMatcher.cs @@ -503,9 +503,8 @@ protected internal virtual IParseTree MatchImpl(IParseTree tree, IParseTree patt /// protected internal virtual RuleTagToken GetRuleTagToken(IParseTree t) { - if (t is IRuleNode) + if (t is IRuleNode r) { - IRuleNode r = (IRuleNode)t; if (r.ChildCount == 1 && r.GetChild(0) is ITerminalNode) { ITerminalNode c = (ITerminalNode)r.GetChild(0); @@ -527,9 +526,8 @@ public virtual IList Tokenize(string pattern) IList tokens = new List(); foreach (Chunk chunk in chunks) { - if (chunk is TagChunk) + if (chunk is TagChunk tagChunk) { - TagChunk tagChunk = (TagChunk)chunk; // add special rule token or conjure up new token from name if (System.Char.IsUpper(tagChunk.Tag[0])) { @@ -690,9 +688,8 @@ internal virtual IList Split(string pattern) for (int i_2 = 0; i_2 < chunks.Count; i_2++) { Chunk c = chunks[i_2]; - if (c is TextChunk) + if (c is TextChunk tc) { - TextChunk tc = (TextChunk)c; string unescaped = tc.Text.Replace(escape, string.Empty); if (unescaped.Length < tc.Text.Length) { diff --git a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Trees.cs b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Trees.cs index 829cb55240..5a2a8b3310 100644 --- a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Trees.cs +++ b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Trees.cs @@ -87,9 +87,9 @@ public static string GetNodeText(ITree t, IList ruleNames) { if (ruleNames != null) { - if (t is RuleContext) + if (t is RuleContext ruleContext) { - int ruleIndex = ((RuleContext)t).RuleIndex; + int ruleIndex = ruleContext.RuleIndex; string ruleName = ruleNames[ruleIndex]; int altNumber = ((RuleContext)t).getAltNumber(); if ( altNumber!=Atn.ATN.INVALID_ALT_NUMBER ) { @@ -105,9 +105,9 @@ public static string GetNodeText(ITree t, IList ruleNames) } else { - if (t is ITerminalNode) + if (t is ITerminalNode iTerminalNode) { - IToken symbol = ((ITerminalNode)t).Symbol; + IToken symbol = iTerminalNode.Symbol; if (symbol != null) { string s = symbol.Text; @@ -119,9 +119,9 @@ public static string GetNodeText(ITree t, IList ruleNames) } // no recog for rule names object payload = t.Payload; - if (payload is IToken) + if (payload is IToken iToken) { - return ((IToken)payload).Text; + return iToken.Text; } return t.Payload.ToString(); } diff --git a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Xpath/XPathRuleElement.cs b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Xpath/XPathRuleElement.cs index bbea5f38af..b6da16a334 100644 --- a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Xpath/XPathRuleElement.cs +++ b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Xpath/XPathRuleElement.cs @@ -29,9 +29,8 @@ public override ICollection Evaluate(IParseTree t) IList nodes = new List(); foreach (ITree c in Trees.GetChildren(t)) { - if (c is ParserRuleContext) + if (c is ParserRuleContext ctx) { - ParserRuleContext ctx = (ParserRuleContext)c; if ((ctx.RuleIndex == ruleIndex && !invert) || (ctx.RuleIndex != ruleIndex && invert)) { nodes.Add(ctx); diff --git a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Xpath/XPathTokenElement.cs b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Xpath/XPathTokenElement.cs index 7da9e97a37..f892a1b5bb 100644 --- a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Xpath/XPathTokenElement.cs +++ b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Xpath/XPathTokenElement.cs @@ -28,9 +28,8 @@ public override ICollection Evaluate(IParseTree t) IList nodes = new List(); foreach (ITree c in Trees.GetChildren(t)) { - if (c is ITerminalNode) + if (c is ITerminalNode tnode) { - ITerminalNode tnode = (ITerminalNode)c; if ((tnode.Symbol.Type == tokenType && !invert) || (tnode.Symbol.Type != tokenType && invert)) { nodes.Add(tnode); diff --git a/Microsoft.Azure.Cosmos/src/OSS/Antlr/UnbufferedTokenStream.cs b/Microsoft.Azure.Cosmos/src/OSS/Antlr/UnbufferedTokenStream.cs index 136082aea8..17734a2978 100644 --- a/Microsoft.Azure.Cosmos/src/OSS/Antlr/UnbufferedTokenStream.cs +++ b/Microsoft.Azure.Cosmos/src/OSS/Antlr/UnbufferedTokenStream.cs @@ -265,9 +265,9 @@ protected internal virtual void Add(IToken t) { tokens = Arrays.CopyOf(tokens, tokens.Length * 2); } - if (t is IWritableToken) + if (t is IWritableToken iWritableToken) { - ((IWritableToken)t).TokenIndex = GetBufferStartIndex() + n; + iWritableToken.TokenIndex = GetBufferStartIndex() + n; } tokens[n++] = t; } diff --git a/Microsoft.Azure.Cosmos/src/ResourceThrottleRetryPolicy.cs b/Microsoft.Azure.Cosmos/src/ResourceThrottleRetryPolicy.cs index d8d1d4c885..c6187f3e51 100644 --- a/Microsoft.Azure.Cosmos/src/ResourceThrottleRetryPolicy.cs +++ b/Microsoft.Azure.Cosmos/src/ResourceThrottleRetryPolicy.cs @@ -48,9 +48,8 @@ public Task ShouldRetryAsync( Exception exception, CancellationToken cancellationToken) { - if (exception is DocumentClientException) + if (exception is DocumentClientException dce) { - DocumentClientException dce = (DocumentClientException)exception; if (!this.IsValidThrottleStatusCode(dce.StatusCode)) { DefaultTrace.TraceError( From f989eb29094ae741d652ea4ff622dd3de941ec85 Mon Sep 17 00:00:00 2001 From: marodev Date: Fri, 14 May 2021 23:42:28 +0200 Subject: [PATCH 2/5] Revert "Fix SonarQube's "Duplicate casts should not be made" / Code Smell" This reverts commit e351e56edcfb485751c3ba88b8489bc8d3c554b2. --- .../src/OSS/Antlr/Atn/LL1Analyzer.cs | 3 ++- .../src/OSS/Antlr/Atn/LexerActionExecutor.cs | 4 ++-- .../src/OSS/Antlr/Atn/ParserATNSimulator.cs | 6 ++++-- .../src/OSS/Antlr/BufferedTokenStream.cs | 4 ++-- .../src/OSS/Antlr/FailedPredicateException.cs | 4 ++-- .../src/OSS/Antlr/Misc/IntervalSet.cs | 3 ++- Microsoft.Azure.Cosmos/src/OSS/Antlr/Parser.cs | 3 ++- .../src/OSS/Antlr/ParserRuleContext.cs | 6 ++++-- .../Antlr/Tree/Pattern/ParseTreePatternMatcher.cs | 9 ++++++--- Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Trees.cs | 12 ++++++------ .../src/OSS/Antlr/Tree/Xpath/XPathRuleElement.cs | 3 ++- .../src/OSS/Antlr/Tree/Xpath/XPathTokenElement.cs | 3 ++- .../src/OSS/Antlr/UnbufferedTokenStream.cs | 4 ++-- .../src/ResourceThrottleRetryPolicy.cs | 3 ++- 14 files changed, 40 insertions(+), 27 deletions(-) diff --git a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Atn/LL1Analyzer.cs b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Atn/LL1Analyzer.cs index db85b3d6ce..6893ab6eb7 100644 --- a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Atn/LL1Analyzer.cs +++ b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Atn/LL1Analyzer.cs @@ -314,8 +314,9 @@ protected internal virtual void Look(ATNState s, ATNState stopState, PredictionC for (int i_1 = 0; i_1 < n; i_1++) { Transition t = s.Transition(i_1); - if (t is RuleTransition ruleTransition) + if (t is RuleTransition) { + RuleTransition ruleTransition = (RuleTransition)t; if (calledRuleStack.Get(ruleTransition.ruleIndex)) { continue; diff --git a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Atn/LexerActionExecutor.cs b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Atn/LexerActionExecutor.cs index 2430c7e434..b485522361 100644 --- a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Atn/LexerActionExecutor.cs +++ b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Atn/LexerActionExecutor.cs @@ -223,9 +223,9 @@ public virtual void Execute(Lexer lexer, ICharStream input, int startIndex) foreach (ILexerAction lexerAction in lexerActions) { ILexerAction action = lexerAction; - if (action is LexerIndexedCustomAction lexerIndexedCustomAction) + if (action is LexerIndexedCustomAction) { - int offset = lexerIndexedCustomAction.Offset; + int offset = ((LexerIndexedCustomAction)action).Offset; input.Seek(startIndex + offset); action = ((LexerIndexedCustomAction)action).Action; requiresSeek = (startIndex + offset) != stopIndex; diff --git a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Atn/ParserATNSimulator.cs b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Atn/ParserATNSimulator.cs index e5f46c6927..59eabbffb6 100644 --- a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Atn/ParserATNSimulator.cs +++ b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Atn/ParserATNSimulator.cs @@ -2124,12 +2124,14 @@ public void DumpDeadEndConfigs(NoViableAltException nvae) if (c.state.NumberOfTransitions > 0) { Transition t = c.state.Transition(0); - if (t is AtomTransition at) + if (t is AtomTransition) { + AtomTransition at = (AtomTransition)t; trans = "Atom " + GetTokenName(at.token); } - else if (t is SetTransition st) + else if (t is SetTransition) { + SetTransition st = (SetTransition)t; bool not = st is NotSetTransition; trans = (not ? "~" : "") + "Set " + st.set.ToString(); } diff --git a/Microsoft.Azure.Cosmos/src/OSS/Antlr/BufferedTokenStream.cs b/Microsoft.Azure.Cosmos/src/OSS/Antlr/BufferedTokenStream.cs index a63e85a5d5..a3215b3d49 100644 --- a/Microsoft.Azure.Cosmos/src/OSS/Antlr/BufferedTokenStream.cs +++ b/Microsoft.Azure.Cosmos/src/OSS/Antlr/BufferedTokenStream.cs @@ -241,9 +241,9 @@ protected internal virtual int Fetch(int n) for (int i = 0; i < n; i++) { IToken t = _tokenSource.NextToken(); - if (t is IWritableToken iWritableToken) + if (t is IWritableToken) { - iWritableToken.TokenIndex = tokens.Count; + ((IWritableToken)t).TokenIndex = tokens.Count; } tokens.Add(t); if (t.Type == TokenConstants.EOF) diff --git a/Microsoft.Azure.Cosmos/src/OSS/Antlr/FailedPredicateException.cs b/Microsoft.Azure.Cosmos/src/OSS/Antlr/FailedPredicateException.cs index 2dc71c689f..f9c819766e 100644 --- a/Microsoft.Azure.Cosmos/src/OSS/Antlr/FailedPredicateException.cs +++ b/Microsoft.Azure.Cosmos/src/OSS/Antlr/FailedPredicateException.cs @@ -46,9 +46,9 @@ public FailedPredicateException(Parser recognizer, string predicate, string mess { ATNState s = recognizer.Interpreter.atn.states[recognizer.State]; AbstractPredicateTransition trans = (AbstractPredicateTransition)s.Transition(0); - if (trans is PredicateTransition predicateTransition) + if (trans is PredicateTransition) { - this.ruleIndex = predicateTransition.ruleIndex; + this.ruleIndex = ((PredicateTransition)trans).ruleIndex; this.predicateIndex = ((PredicateTransition)trans).predIndex; } else diff --git a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Misc/IntervalSet.cs b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Misc/IntervalSet.cs index 08929c918e..dd520a27ec 100644 --- a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Misc/IntervalSet.cs +++ b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Misc/IntervalSet.cs @@ -206,8 +206,9 @@ public virtual Antlr4.Runtime.Misc.IntervalSet AddAll(IIntSet set) { return this; } - if (set is Antlr4.Runtime.Misc.IntervalSet other) + if (set is Antlr4.Runtime.Misc.IntervalSet) { + Antlr4.Runtime.Misc.IntervalSet other = (Antlr4.Runtime.Misc.IntervalSet)set; // walk set and add each interval int n = other.intervals.Count; for (int i = 0; i < n; i++) diff --git a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Parser.cs b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Parser.cs index fe3519f204..7fafedfbc6 100644 --- a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Parser.cs +++ b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Parser.cs @@ -604,8 +604,9 @@ public virtual ParseTreePattern CompileParseTreePattern(string pattern, int patt if (((ITokenStream)InputStream) != null) { ITokenSource tokenSource = ((ITokenStream)InputStream).TokenSource; - if (tokenSource is Lexer lexer) + if (tokenSource is Lexer) { + Lexer lexer = (Lexer)tokenSource; return CompileParseTreePattern(pattern, patternRuleIndex, lexer); } } diff --git a/Microsoft.Azure.Cosmos/src/OSS/Antlr/ParserRuleContext.cs b/Microsoft.Azure.Cosmos/src/OSS/Antlr/ParserRuleContext.cs index ae62075c09..5606464acf 100644 --- a/Microsoft.Azure.Cosmos/src/OSS/Antlr/ParserRuleContext.cs +++ b/Microsoft.Azure.Cosmos/src/OSS/Antlr/ParserRuleContext.cs @@ -265,8 +265,9 @@ public virtual ITerminalNode GetToken(int ttype, int i) // what token with ttype have we found? foreach (IParseTree o in children) { - if (o is ITerminalNode tnode) + if (o is ITerminalNode) { + ITerminalNode tnode = (ITerminalNode)o; IToken symbol = tnode.Symbol; if (symbol.Type == ttype) { @@ -294,8 +295,9 @@ public virtual ITerminalNode[] GetTokens(int ttype) List tokens = null; foreach (IParseTree o in children) { - if (o is ITerminalNode tnode) + if (o is ITerminalNode) { + ITerminalNode tnode = (ITerminalNode)o; IToken symbol = tnode.Symbol; if (symbol.Type == ttype) { diff --git a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Pattern/ParseTreePatternMatcher.cs b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Pattern/ParseTreePatternMatcher.cs index a3c9c6e07e..591a991721 100644 --- a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Pattern/ParseTreePatternMatcher.cs +++ b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Pattern/ParseTreePatternMatcher.cs @@ -503,8 +503,9 @@ protected internal virtual IParseTree MatchImpl(IParseTree tree, IParseTree patt /// protected internal virtual RuleTagToken GetRuleTagToken(IParseTree t) { - if (t is IRuleNode r) + if (t is IRuleNode) { + IRuleNode r = (IRuleNode)t; if (r.ChildCount == 1 && r.GetChild(0) is ITerminalNode) { ITerminalNode c = (ITerminalNode)r.GetChild(0); @@ -526,8 +527,9 @@ public virtual IList Tokenize(string pattern) IList tokens = new List(); foreach (Chunk chunk in chunks) { - if (chunk is TagChunk tagChunk) + if (chunk is TagChunk) { + TagChunk tagChunk = (TagChunk)chunk; // add special rule token or conjure up new token from name if (System.Char.IsUpper(tagChunk.Tag[0])) { @@ -688,8 +690,9 @@ internal virtual IList Split(string pattern) for (int i_2 = 0; i_2 < chunks.Count; i_2++) { Chunk c = chunks[i_2]; - if (c is TextChunk tc) + if (c is TextChunk) { + TextChunk tc = (TextChunk)c; string unescaped = tc.Text.Replace(escape, string.Empty); if (unescaped.Length < tc.Text.Length) { diff --git a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Trees.cs b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Trees.cs index 5a2a8b3310..829cb55240 100644 --- a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Trees.cs +++ b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Trees.cs @@ -87,9 +87,9 @@ public static string GetNodeText(ITree t, IList ruleNames) { if (ruleNames != null) { - if (t is RuleContext ruleContext) + if (t is RuleContext) { - int ruleIndex = ruleContext.RuleIndex; + int ruleIndex = ((RuleContext)t).RuleIndex; string ruleName = ruleNames[ruleIndex]; int altNumber = ((RuleContext)t).getAltNumber(); if ( altNumber!=Atn.ATN.INVALID_ALT_NUMBER ) { @@ -105,9 +105,9 @@ public static string GetNodeText(ITree t, IList ruleNames) } else { - if (t is ITerminalNode iTerminalNode) + if (t is ITerminalNode) { - IToken symbol = iTerminalNode.Symbol; + IToken symbol = ((ITerminalNode)t).Symbol; if (symbol != null) { string s = symbol.Text; @@ -119,9 +119,9 @@ public static string GetNodeText(ITree t, IList ruleNames) } // no recog for rule names object payload = t.Payload; - if (payload is IToken iToken) + if (payload is IToken) { - return iToken.Text; + return ((IToken)payload).Text; } return t.Payload.ToString(); } diff --git a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Xpath/XPathRuleElement.cs b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Xpath/XPathRuleElement.cs index b6da16a334..bbea5f38af 100644 --- a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Xpath/XPathRuleElement.cs +++ b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Xpath/XPathRuleElement.cs @@ -29,8 +29,9 @@ public override ICollection Evaluate(IParseTree t) IList nodes = new List(); foreach (ITree c in Trees.GetChildren(t)) { - if (c is ParserRuleContext ctx) + if (c is ParserRuleContext) { + ParserRuleContext ctx = (ParserRuleContext)c; if ((ctx.RuleIndex == ruleIndex && !invert) || (ctx.RuleIndex != ruleIndex && invert)) { nodes.Add(ctx); diff --git a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Xpath/XPathTokenElement.cs b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Xpath/XPathTokenElement.cs index f892a1b5bb..7da9e97a37 100644 --- a/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Xpath/XPathTokenElement.cs +++ b/Microsoft.Azure.Cosmos/src/OSS/Antlr/Tree/Xpath/XPathTokenElement.cs @@ -28,8 +28,9 @@ public override ICollection Evaluate(IParseTree t) IList nodes = new List(); foreach (ITree c in Trees.GetChildren(t)) { - if (c is ITerminalNode tnode) + if (c is ITerminalNode) { + ITerminalNode tnode = (ITerminalNode)c; if ((tnode.Symbol.Type == tokenType && !invert) || (tnode.Symbol.Type != tokenType && invert)) { nodes.Add(tnode); diff --git a/Microsoft.Azure.Cosmos/src/OSS/Antlr/UnbufferedTokenStream.cs b/Microsoft.Azure.Cosmos/src/OSS/Antlr/UnbufferedTokenStream.cs index 17734a2978..136082aea8 100644 --- a/Microsoft.Azure.Cosmos/src/OSS/Antlr/UnbufferedTokenStream.cs +++ b/Microsoft.Azure.Cosmos/src/OSS/Antlr/UnbufferedTokenStream.cs @@ -265,9 +265,9 @@ protected internal virtual void Add(IToken t) { tokens = Arrays.CopyOf(tokens, tokens.Length * 2); } - if (t is IWritableToken iWritableToken) + if (t is IWritableToken) { - iWritableToken.TokenIndex = GetBufferStartIndex() + n; + ((IWritableToken)t).TokenIndex = GetBufferStartIndex() + n; } tokens[n++] = t; } diff --git a/Microsoft.Azure.Cosmos/src/ResourceThrottleRetryPolicy.cs b/Microsoft.Azure.Cosmos/src/ResourceThrottleRetryPolicy.cs index c6187f3e51..d8d1d4c885 100644 --- a/Microsoft.Azure.Cosmos/src/ResourceThrottleRetryPolicy.cs +++ b/Microsoft.Azure.Cosmos/src/ResourceThrottleRetryPolicy.cs @@ -48,8 +48,9 @@ public Task ShouldRetryAsync( Exception exception, CancellationToken cancellationToken) { - if (exception is DocumentClientException dce) + if (exception is DocumentClientException) { + DocumentClientException dce = (DocumentClientException)exception; if (!this.IsValidThrottleStatusCode(dce.StatusCode)) { DefaultTrace.TraceError( From 035149992bc92ebfc02507960718c54b17abe782 Mon Sep 17 00:00:00 2001 From: marodev Date: Fri, 14 May 2021 23:51:39 +0200 Subject: [PATCH 3/5] Fix SonarQube's "Duplicate casts should not be made" / Code Smell --- Microsoft.Azure.Cosmos/src/ResourceThrottleRetryPolicy.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Microsoft.Azure.Cosmos/src/ResourceThrottleRetryPolicy.cs b/Microsoft.Azure.Cosmos/src/ResourceThrottleRetryPolicy.cs index d8d1d4c885..c6187f3e51 100644 --- a/Microsoft.Azure.Cosmos/src/ResourceThrottleRetryPolicy.cs +++ b/Microsoft.Azure.Cosmos/src/ResourceThrottleRetryPolicy.cs @@ -48,9 +48,8 @@ public Task ShouldRetryAsync( Exception exception, CancellationToken cancellationToken) { - if (exception is DocumentClientException) + if (exception is DocumentClientException dce) { - DocumentClientException dce = (DocumentClientException)exception; if (!this.IsValidThrottleStatusCode(dce.StatusCode)) { DefaultTrace.TraceError( From 4abccc5bbd8f5c6faa982f320579a09e8193f294 Mon Sep 17 00:00:00 2001 From: marodev Date: Sun, 16 May 2021 11:00:55 +0200 Subject: [PATCH 4/5] Fix ReSharper's "UseNullPropagation" --- .../src/Custom/AeadAes256CbcHmac256Algorithm.cs | 10 ++-------- Microsoft.Azure.Cosmos/src/DocumentClient.cs | 10 ++-------- .../src/Routing/ClientCollectionCache.cs | 5 +---- .../Microsoft.Azure.Cosmos.EmulatorTests/Utils/Util.cs | 10 ++-------- 4 files changed, 7 insertions(+), 28 deletions(-) diff --git a/Microsoft.Azure.Cosmos.Encryption/src/Custom/AeadAes256CbcHmac256Algorithm.cs b/Microsoft.Azure.Cosmos.Encryption/src/Custom/AeadAes256CbcHmac256Algorithm.cs index 596e272d9a..df416c3efd 100644 --- a/Microsoft.Azure.Cosmos.Encryption/src/Custom/AeadAes256CbcHmac256Algorithm.cs +++ b/Microsoft.Azure.Cosmos.Encryption/src/Custom/AeadAes256CbcHmac256Algorithm.cs @@ -198,10 +198,7 @@ protected byte[] EncryptData(byte[] plainText, bool hasAuthenticationTag) } catch (Exception) { - if (aesAlg != null) - { - aesAlg.Dispose(); - } + aesAlg?.Dispose(); throw; } @@ -352,10 +349,7 @@ private byte[] DecryptData(byte[] iv, byte[] cipherText, int offset, int count) } catch (Exception) { - if (aesAlg != null) - { - aesAlg.Dispose(); - } + aesAlg?.Dispose(); throw; } diff --git a/Microsoft.Azure.Cosmos/src/DocumentClient.cs b/Microsoft.Azure.Cosmos/src/DocumentClient.cs index 9bead640e5..45ec33089b 100644 --- a/Microsoft.Azure.Cosmos/src/DocumentClient.cs +++ b/Microsoft.Azure.Cosmos/src/DocumentClient.cs @@ -1353,10 +1353,7 @@ internal async Task ProcessRequestAsync( { await this.EnsureValidClientAsync(NoOpTrace.Singleton); - if (retryPolicyInstance != null) - { - retryPolicyInstance.OnBeforeSendRequest(request); - } + retryPolicyInstance?.OnBeforeSendRequest(request); using (new ActivityScope(Guid.NewGuid())) { @@ -5487,10 +5484,7 @@ await this.AddPartitionKeyInformationAsync( options); } - if (retryPolicyInstance != null) - { - retryPolicyInstance.OnBeforeSendRequest(request); - } + retryPolicyInstance?.OnBeforeSendRequest(request); request.SerializerSettings = this.GetSerializerSettingsForRequest(options); return new StoredProcedureResponse(await this.ExecuteProcedureAsync( diff --git a/Microsoft.Azure.Cosmos/src/Routing/ClientCollectionCache.cs b/Microsoft.Azure.Cosmos/src/Routing/ClientCollectionCache.cs index 90ef684db0..157324a84e 100644 --- a/Microsoft.Azure.Cosmos/src/Routing/ClientCollectionCache.cs +++ b/Microsoft.Azure.Cosmos/src/Routing/ClientCollectionCache.cs @@ -100,10 +100,7 @@ private async Task ReadCollectionAsync(string collectionLin using (new ActivityScope(Guid.NewGuid())) { - if (retryPolicyInstance != null) - { - retryPolicyInstance.OnBeforeSendRequest(request); - } + retryPolicyInstance?.OnBeforeSendRequest(request); try { diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/Utils/Util.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/Utils/Util.cs index 822df85b03..b17a575223 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/Utils/Util.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/Utils/Util.cs @@ -120,10 +120,7 @@ internal static void TestForEachClient( } } - if (requestChargeHelper != null) - { - requestChargeHelper.CompareRequestCharge(testName); - } + requestChargeHelper?.CompareRequestCharge(testName); } /// @@ -162,10 +159,7 @@ internal static void TestForAnyClient( client.Dispose(); } - if (requestChargeHelper != null) - { - requestChargeHelper.CompareRequestCharge(testName); - } + requestChargeHelper?.CompareRequestCharge(testName); } private static void RunTestForClient( From 6b165d26b37df80a789efd8691368faa802dfb26 Mon Sep 17 00:00:00 2001 From: marodev Date: Sun, 16 May 2021 11:14:54 +0200 Subject: [PATCH 5/5] Fix ReSharper's "MergeSequentialChecks" --- .../src/Custom/EncryptionProcessor.cs | 2 +- Microsoft.Azure.Cosmos/src/DocumentClient.cs | 12 ++++++------ .../v2Query/DocumentQueryExecutionContextFactory.cs | 2 +- .../src/Routing/CollectionCache.cs | 2 +- .../src/Routing/PartitionRoutingHelper.cs | 5 ++--- .../GatewayTests.cs | 2 +- .../Utils/QueryOracleUtil.cs | 2 +- 7 files changed, 13 insertions(+), 14 deletions(-) diff --git a/Microsoft.Azure.Cosmos.Encryption/src/Custom/EncryptionProcessor.cs b/Microsoft.Azure.Cosmos.Encryption/src/Custom/EncryptionProcessor.cs index 7753fb9eb9..70b316917f 100644 --- a/Microsoft.Azure.Cosmos.Encryption/src/Custom/EncryptionProcessor.cs +++ b/Microsoft.Azure.Cosmos.Encryption/src/Custom/EncryptionProcessor.cs @@ -478,7 +478,7 @@ private static JObject RetrieveEncryptionProperties( { JProperty encryptionPropertiesJProp = item.Property(Constants.EncryptedInfo); JObject encryptionPropertiesJObj = null; - if (encryptionPropertiesJProp != null && encryptionPropertiesJProp.Value != null && encryptionPropertiesJProp.Value.Type == JTokenType.Object) + if (encryptionPropertiesJProp?.Value != null && encryptionPropertiesJProp.Value.Type == JTokenType.Object) { encryptionPropertiesJObj = (JObject)encryptionPropertiesJProp.Value; } diff --git a/Microsoft.Azure.Cosmos/src/DocumentClient.cs b/Microsoft.Azure.Cosmos/src/DocumentClient.cs index 45ec33089b..37630f4529 100644 --- a/Microsoft.Azure.Cosmos/src/DocumentClient.cs +++ b/Microsoft.Azure.Cosmos/src/DocumentClient.cs @@ -1725,7 +1725,7 @@ public Task> CreateDocumentAsync(string documentsFeed private async Task> CreateDocumentInlineAsync(string documentsFeedOrDatabaseLink, object document, Documents.Client.RequestOptions options, bool disableAutomaticIdGeneration, CancellationToken cancellationToken) { IDocumentClientRetryPolicy requestRetryPolicy = this.ResetSessionTokenRetryPolicy.GetRequestPolicy(); - if (options == null || options.PartitionKey == null) + if (options?.PartitionKey == null) { requestRetryPolicy = new PartitionKeyMismatchRetryPolicy( await this.GetCollectionCacheAsync(NoOpTrace.Singleton), @@ -5477,7 +5477,7 @@ private async Task> ExecuteStoredProcedurePrivat headers)) { request.Headers[HttpConstants.HttpHeaders.XDate] = DateTime.UtcNow.ToString("r"); - if (options == null || options.PartitionKeyRangeId == null) + if (options?.PartitionKeyRangeId == null) { await this.AddPartitionKeyInformationAsync( request, @@ -5677,7 +5677,7 @@ public Task> UpsertDocumentAsync(string documentsFeed private async Task> UpsertDocumentInlineAsync(string documentsFeedOrDatabaseLink, object document, Documents.Client.RequestOptions options, bool disableAutomaticIdGeneration, CancellationToken cancellationToken) { IDocumentClientRetryPolicy requestRetryPolicy = this.ResetSessionTokenRetryPolicy.GetRequestPolicy(); - if (options == null || options.PartitionKey == null) + if (options?.PartitionKey == null) { requestRetryPolicy = new PartitionKeyMismatchRetryPolicy( await this.GetCollectionCacheAsync(NoOpTrace.Singleton), @@ -6640,11 +6640,11 @@ private async Task AddPartitionKeyInformationAsync(DocumentServiceRequest reques PartitionKeyDefinition partitionKeyDefinition = collection.PartitionKey; PartitionKeyInternal partitionKey; - if (options != null && options.PartitionKey != null && options.PartitionKey.Equals(Documents.PartitionKey.None)) + if (options?.PartitionKey != null && options.PartitionKey.Equals(Documents.PartitionKey.None)) { partitionKey = collection.GetNoneValue(); } - else if (options != null && options.PartitionKey != null) + else if (options?.PartitionKey != null) { partitionKey = options.PartitionKey.InternalKey; } @@ -6665,7 +6665,7 @@ internal async Task AddPartitionKeyInformationAsync(DocumentServiceRequest reque // For backward compatibility, if collection doesn't have partition key defined, we assume all documents // have empty value for it and user doesn't need to specify it explicitly. PartitionKeyInternal partitionKey; - if (options == null || options.PartitionKey == null) + if (options?.PartitionKey == null) { if (partitionKeyDefinition == null || partitionKeyDefinition.Paths.Count == 0) { diff --git a/Microsoft.Azure.Cosmos/src/Query/v2Query/DocumentQueryExecutionContextFactory.cs b/Microsoft.Azure.Cosmos/src/Query/v2Query/DocumentQueryExecutionContextFactory.cs index 3501470f83..63f17406c5 100644 --- a/Microsoft.Azure.Cosmos/src/Query/v2Query/DocumentQueryExecutionContextFactory.cs +++ b/Microsoft.Azure.Cosmos/src/Query/v2Query/DocumentQueryExecutionContextFactory.cs @@ -46,7 +46,7 @@ public static async Task CreateDocumentQueryExec collection = await collectionCache.ResolveCollectionAsync(request, token, NoOpTrace.Singleton); } - if (feedOptions != null && feedOptions.PartitionKey != null && feedOptions.PartitionKey.Equals(Documents.PartitionKey.None)) + if (feedOptions?.PartitionKey != null && feedOptions.PartitionKey.Equals(Documents.PartitionKey.None)) { feedOptions.PartitionKey = Documents.PartitionKey.FromInternalKey(collection.GetNoneValue()); } diff --git a/Microsoft.Azure.Cosmos/src/Routing/CollectionCache.cs b/Microsoft.Azure.Cosmos/src/Routing/CollectionCache.cs index 750b376302..32a83ffed7 100644 --- a/Microsoft.Azure.Cosmos/src/Routing/CollectionCache.cs +++ b/Microsoft.Azure.Cosmos/src/Routing/CollectionCache.cs @@ -220,7 +220,7 @@ private async Task ResolveByPartitionKeyRangeIdentityAsync( { // if request is targeted at specific partition using x-ms-documentd-partitionkeyrangeid header, // which contains value ",", then resolve to collection rid in this header. - if (partitionKeyRangeIdentity != null && partitionKeyRangeIdentity.CollectionRid != null) + if (partitionKeyRangeIdentity?.CollectionRid != null) { try { diff --git a/Microsoft.Azure.Cosmos/src/Routing/PartitionRoutingHelper.cs b/Microsoft.Azure.Cosmos/src/Routing/PartitionRoutingHelper.cs index c645d4cc55..89df77afd1 100644 --- a/Microsoft.Azure.Cosmos/src/Routing/PartitionRoutingHelper.cs +++ b/Microsoft.Azure.Cosmos/src/Routing/PartitionRoutingHelper.cs @@ -66,8 +66,7 @@ public static IReadOnlyList> GetProvidedPartitionKeyRanges( } PartitionedQueryExecutionInfo queryExecutionInfo = tryGetPartitionQueryExecutionInfo.Result; - if (queryExecutionInfo == null || - queryExecutionInfo.QueryRanges == null || + if (queryExecutionInfo?.QueryRanges == null || queryExecutionInfo.QueryInfo == null || queryExecutionInfo.QueryRanges.Any(range => range.Min == null || range.Max == null)) { @@ -435,7 +434,7 @@ public virtual Range ExtractPartitionKeyRangeFromContinuationToken(IName } } - if (initialContinuationToken != null && initialContinuationToken.Range != null) + if (initialContinuationToken?.Range != null) { range = initialContinuationToken.Range; } diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/GatewayTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/GatewayTests.cs index 527cb0351e..f63d52429c 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/GatewayTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/GatewayTests.cs @@ -3413,7 +3413,7 @@ public static string DumpFullExceptionMessage(Exception e) while (e != null) { DocumentClientException docException = e as DocumentClientException; - if (docException != null && docException.Error != null) + if (docException?.Error != null) { exceptionMessage.Append("Code : " + docException.Error.Code); if (docException.Error.ErrorDetails != null) diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/Utils/QueryOracleUtil.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/Utils/QueryOracleUtil.cs index 7f69e8f017..4f77603358 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/Utils/QueryOracleUtil.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/Utils/QueryOracleUtil.cs @@ -463,7 +463,7 @@ public Query(string strFilter, string rootName = "r", FieldComparer comparer = n if (filterBuffer.Length > 0) this.query += " WHERE " + filterBuffer.ToString(); - if (comparer != null && comparer.field != null && comparer.order != 0) + if (comparer?.field != null && comparer.order != 0) { this.query += " ORDER BY r." + comparer.field.Name + (comparer.order > 0 ? " ASC" : " DESC"); this.Comparer = comparer;