You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
New rule - "Use single quotes unless you need double quotes to enclose expressions or variables" #292
changed the title [-]Add support for "Use single quotes unless you need double quotes to enclose expressions or variables."[/-][+]New rule - "Use single quotes unless you need double quotes to enclose expressions or variables"[/+]on Aug 17, 2015
Hi Zuldan.
We are focusing on adding functionality based rules to ScriptAnalyzer.
"Style" rules (such as using specific casing, syntax, quotes) need to be part of the community driven Style Rules, which can ship outside our core set of rules.
I had the same thought and I like to support this rule as well, especially because it is not just a "style" rule, according to this site: Powershell and Single vs Double Quotes there is a performance difference. Quote:
double quotes is just over double the execution time of single quotes
I think that the rule should basically apply if the double quoted string doesn't contain any of the following characters:
In fact, if the string itself does not contain special characters like the space, dollar, backtick or single/double quotes then there is no need for a single quotes at all. The PowerShell parser was designed to not require quotes at all.
I agree that a rule against double quoted strings that are not necessary would be useful.
Good point!
Actually, I have some second thoughts with regards to "no need for a single quotes at all" statement, as that is only valid in argument mode not in expression mode.
In expression mode, you always need quotes to define a string, like: $a = 'test'
iRon7 (as discussed on Stack Overflow), while it makes good sense to use double quotes only when expansion is needed - for conceptual clarity and to avoid unwanted expansion - there is virtually no performance difference; the test methodology of the linked blog post is flawed.
Christoph Bergmeister (@bergmeister), warning against potentially unnecessary quotes (in argument mode) can get tricky, because the quotes may be needed in order to pass a "number-like thing" deliberately as a string; e.g., foo '42' passes a string, whereas foo 42 passes a number.
To avoid false positives, you'd have to analyze each quoted argument to see if PowerShell would not parse it as a number, and only then warn if the quotes make no difference.
Activity
[-]Add support for "Use single quotes unless you need double quotes to enclose expressions or variables."[/-][+]New rule - "Use single quotes unless you need double quotes to enclose expressions or variables"[/+]raghushantha commentedon Oct 28, 2015
Hi Zuldan.
We are focusing on adding functionality based rules to ScriptAnalyzer.
"Style" rules (such as using specific casing, syntax, quotes) need to be part of the community driven Style Rules, which can ship outside our core set of rules.
RemcoKapinga commentedon Sep 4, 2016
I would support this new rule!
iRon7 commentedon May 16, 2019
I had the same thought and I like to support this rule as well, especially because it is not just a "style" rule, according to this site: Powershell and Single vs Double Quotes there is a performance difference. Quote:
I think that the rule should basically apply if the double quoted string doesn't contain any of the following characters:
$)')`)bergmeister commentedon May 16, 2019
In fact, if the string itself does not contain special characters like the space, dollar, backtick or single/double quotes then there is no need for a single quotes at all. The PowerShell parser was designed to not require quotes at all.
I agree that a rule against double quoted strings that are not necessary would be useful.
iRon7 commentedon May 17, 2019
Good point!
Actually, I have some second thoughts with regards to "no need for a single quotes at all" statement, as that is only valid in argument mode not in expression mode.
In expression mode, you always need quotes to define a string, like:
$a = 'test'morsley commentedon May 17, 2019
Raghu Shantha (@raghushantha) I don't think this is a style-based rule, but one based on functionality.
mklement0 commentedon Mar 10, 2020
iRon7 (as discussed on Stack Overflow), while it makes good sense to use double quotes only when expansion is needed - for conceptual clarity and to avoid unwanted expansion - there is virtually no performance difference; the test methodology of the linked blog post is flawed.
mklement0 commentedon Mar 10, 2020
Christoph Bergmeister (@bergmeister), warning against potentially unnecessary quotes (in argument mode) can get tricky, because the quotes may be needed in order to pass a "number-like thing" deliberately as a string; e.g.,
foo '42'passes a string, whereasfoo 42passes a number.To avoid false positives, you'd have to analyze each quoted argument to see if PowerShell would not parse it as a number, and only then warn if the quotes make no difference.