A warning should appear for strings that have double quotes with no expressions or variables.
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 would support this new rule!
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'
@raghushantha I don't think this is a style-based rule, but one based on functionality.
@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.
@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.