Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive SC2094 #2604

Open
2 of 4 tasks
hgl opened this issue Oct 11, 2022 · 2 comments
Open
2 of 4 tasks

False positive SC2094 #2604

hgl opened this issue Oct 11, 2022 · 2 comments

Comments

@hgl
Copy link

hgl commented Oct 11, 2022

For bugs

For new checks and feature suggestions

Here's a snippet or screenshot that shows the problem:

while read -r args; do
  set -- $args
  for f; do
    [ -d "$f" ]
  done
done <"$f"

Here's what shellcheck currently says:

Line 4:
    [ -d "$f" ]
         ^-- [SC2094](https://www.shellcheck.net/wiki/SC2094) (info): Make sure not to read and write the same file in the same pipeline.
 
Line 6:
done <"$f"
      ^-- [SC2094](https://www.shellcheck.net/wiki/SC2094) (info): Make sure not to read and write the same file in the same pipeline.

Here's what I wanted or expected to see:

No warning, since there is really no writing to f.

@trothwell
Copy link

Is the form of the sample the best way to handle this pattern? Are there alternatives?

@hgl
Copy link
Author

hgl commented Oct 14, 2022

The "form" boils down to reusing a one-time redirected variable, which I personally incline to favor if the variable is only temporary.

This sample can be simplified and generates the same false positive:

#!/bin/sh

while read -r f; do
  "$f"
done <"$f"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants