Skip to content

fix: resolve 5 SonarQube issues (unused variable and regex patterns) - #183

Open
sonarqube-agent[bot] wants to merge 1 commit into
masterfrom
remediate-master-20260601-010106-ce96f921
Open

fix: resolve 5 SonarQube issues (unused variable and regex patterns)#183
sonarqube-agent[bot] wants to merge 1 commit into
masterfrom
remediate-master-20260601-010106-ce96f921

Conversation

@sonarqube-agent

Copy link
Copy Markdown

This PR was automatically created by the Remediation Agent's Scheduled backlog remediation feature.

This PR fixes 5 SonarQube code quality issues: removes an unused variable in voice_state.py by correcting a logic error, and replaces verbose regex character classes ([0-9], [A-Za-z0-9_]) with their concise equivalents (\d, \w) across partial_emoji.py and message.py. These improvements enhance code clarity, eliminate dead code, and ensure consistent regex pattern conventions.

View Project in SonarCloud


Fixed Issues

python:S1481 - Remove the unused local variable "previous_endpoint". • MINORView issue

Location: discord/voice_state.py:311

Why is this an issue?

An unused local variable is a variable that has been declared but is not used anywhere in the block of code where it is defined. It is dead code, contributing to unnecessary complexity and leading to confusion when reading the code. Therefore, it should be removed from your code to maintain clarity and efficiency.

What changed

This hunk fixes the unused local variable 'previous_endpoint' by actually using it in the condition check. Previously, the condition duplicated 'previous_token == self.token' twice, leaving 'previous_endpoint' declared but never referenced. The fix replaces the redundant duplicate check with 'previous_endpoint == self.endpoint', which both corrects the logic and eliminates the unused variable warning.

--- a/discord/voice_state.py
+++ b/discord/voice_state.py
@@ -347,1 +347,1 @@ class VoiceConnectionState:
-            if previous_token == self.token and previous_server_id == self.server_id and previous_token == self.token:
+            if previous_token == self.token and previous_server_id == self.server_id and previous_endpoint == self.endpoint:
python:S6353 - Use concise character class syntax '\d' instead of '[0-9]'. • MINORView issue

Location: discord/partial_emoji.py:97

Why is this an issue?

A regular expression is a sequence of characters that specifies a match pattern in text. Among the most important concepts are:

What changed

This hunk replaces verbose character class syntax with concise equivalents in the regex pattern: [A-Za-z0-9\_] is replaced with \w and [0-9] is replaced with \d. This directly addresses the code smell about using concise character class syntax in regular expressions, making the regex more readable and maintainable.

--- a/discord/partial_emoji.py
+++ b/discord/partial_emoji.py
@@ -97,1 +97,1 @@ class PartialEmoji(_EmojiTag, AssetMixin):
-    _CUSTOM_EMOJI_RE = re.compile(r'<?(?:(?P<animated>a)?:)?(?P<name>[A-Za-z0-9\_]+):(?P<id>[0-9]{13,20})>?')
+    _CUSTOM_EMOJI_RE = re.compile(r'<?(?:(?P<animated>a)?:)?(?P<name>\w+):(?P<id>\d{13,20})>?')
python:S6353 - Use concise character class syntax '\d' instead of '[0-9]'. • MINORView issue

Location: discord/message.py:2130

Why is this an issue?

A regular expression is a sequence of characters that specifies a match pattern in text. Among the most important concepts are:

What changed

Replaces the character class '[0-9]' with the more concise '\d' shorthand in the regex pattern on line 2144 of discord/message.py. This addresses the static analysis warning about using concise character class syntax in the role mention parsing regex '<@&([0-9]{15,20})>', making it '<@&(\d{15,20})>'. This also helps address any additional warnings about using '[0-9]' instead of '\d' in regex patterns within this file, as all three hunks together ensure consistent use of the concise '\d' syntax across all mention-parsing regexes.

--- a/discord/message.py
+++ b/discord/message.py
@@ -2144,1 +2144,1 @@ class Message(PartialMessage, Hashable):
-        return [int(x) for x in re.findall(r'<@&([0-9]{15,20})>', self.content)]
+        return [int(x) for x in re.findall(r'<@&(\d{15,20})>', self.content)]
python:S6353 - Use concise character class syntax '\d' instead of '[0-9]'. • MINORView issue

Location: discord/message.py:2137

Why is this an issue?

A regular expression is a sequence of characters that specifies a match pattern in text. Among the most important concepts are:

What changed

Replaces the character class '[0-9]' with the more concise '\d' shorthand in the regex pattern on line 2144 of discord/message.py. This addresses the static analysis warning about using concise character class syntax in the role mention parsing regex '<@&([0-9]{15,20})>', making it '<@&(\d{15,20})>'. This also helps address any additional warnings about using '[0-9]' instead of '\d' in regex patterns within this file, as all three hunks together ensure consistent use of the concise '\d' syntax across all mention-parsing regexes.

--- a/discord/message.py
+++ b/discord/message.py
@@ -2144,1 +2144,1 @@ class Message(PartialMessage, Hashable):
-        return [int(x) for x in re.findall(r'<@&([0-9]{15,20})>', self.content)]
+        return [int(x) for x in re.findall(r'<@&(\d{15,20})>', self.content)]
python:S6353 - Use concise character class syntax '\d' instead of '[0-9]'. • MINORView issue

Location: discord/message.py:2144

Why is this an issue?

A regular expression is a sequence of characters that specifies a match pattern in text. Among the most important concepts are:

What changed

Replaces the character class '[0-9]' with the more concise '\d' shorthand in the regex pattern on line 2137 of discord/message.py. This addresses the static analysis warning about using concise character class syntax in the channel mention parsing regex '<#([0-9]{15,20})>', making it '<#(\d{15,20})>'.

--- a/discord/message.py
+++ b/discord/message.py
@@ -2137,1 +2137,1 @@ class Message(PartialMessage, Hashable):
-        return [int(x) for x in re.findall(r'<#([0-9]{15,20})>', self.content)]
+        return [int(x) for x in re.findall(r'<#(\d{15,20})>', self.content)]

Have a suggestion or found an issue? Share your feedback here.


SonarQube Remediation Agent uses AI. Check for mistakes.

Fixed issues:
- AZUnUZ89U1G2OajCqWs2 for python:S6353 rule
- AZUnUZ3iU1G2OajCqWsL for python:S1481 rule
- AZUnUZ32U1G2OajCqWsT for python:S6353 rule
- AZUnUZ32U1G2OajCqWsV for python:S6353 rule
- AZUnUZ32U1G2OajCqWsU for python:S6353 rule

Generated by SonarQube Agent (task: b366c873-a5db-444b-addb-aace6b72df18)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant