Skip to content

New rule - "Use single quotes unless you need double quotes to enclose expressions or variables" #292

Closed

Description

@Zuldan

A warning should appear for strings that have double quotes with no expressions or variables.

Activity

  1. 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
  2. raghushantha commented on Oct 28, 2015

    @raghushantha
    Member

    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.

  3. RemcoKapinga commented on Sep 4, 2016

    @RemcoKapinga

    I would support this new rule!

  4. iRon7 commented on May 16, 2019

    @iRon7
    Contributor

    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:

    • a Dollar sign ($)
    • a single quote (')
    • a backtick (`)
  5. bergmeister commented on May 16, 2019

    @bergmeister
    Collaborator

    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.

  6. iRon7 commented on May 17, 2019

    @iRon7
    Contributor

    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'

  7. morsley commented on May 17, 2019

    @morsley
    Contributor

    Raghu Shantha (@raghushantha) I don't think this is a style-based rule, but one based on functionality.

  8. mklement0 commented on Mar 10, 2020

    @mklement0

    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.

  9. mklement0 commented on Mar 10, 2020

    @mklement0

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions