-
Notifications
You must be signed in to change notification settings - Fork 17
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
feat: tempfile without flush or close #101
feat: tempfile without flush or close #101
Conversation
title: Warning for tempfile without flush | ||
--- | ||
|
||
"Be cautious when using `$F.name` without preceding it with `.flush()` or `.close()`, as it may result in an error. This is because the file referenced by `$F.name` might not exist at the time of use. To prevent issues, ensure that you either call `.flush()` to write any buffered data to the file or close the file with .close() before referencing `$F.name`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't just copy and paste quotes from the docs. At the very least the leading quote mark isn't helpful and confuses this.
@@ -0,0 +1,227 @@ | |||
--- | |||
title: Warning for tempfile without flush |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use a shorter file name. And don't call it a "warning" - the level of a diagnostic is decided by the consumer: https://docs.grit.io/guides/config#patterns
$fileOpen <: not contains `$f.flush()`, | ||
$fileOpen <: not contains `$f.close()`, | ||
$fileOpen <: contains `$f.name`, | ||
$fileOpen => `# BAD: should add $f.flush() or $f.close() before calling $f.name \n $fileOpen` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't manually add annotations like this via rewrites. Grit already has support for checking things - if no meaningful fix is available, you can skip having a rewrite at all.
In this case, a meaningful autofix might be inserting f.close() on the line before the f.name() call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @morgante
In this case, a meaningful autofix might be inserting f.close() on the line before the f.name() call.
Yes, true I am trying to build the same.
any suggestion, if to build this,
cmd = [binary_name, fout.name, *[str(path) for path in targets]]
parsing this and add $f.close()
before this, become difficult
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to handle all cases. It's fine to skip any that are too complex.
For inserting things it's often best to find the containing statement() and append to that.
Hi @morgante, this PR ready for review. |
Be cautious when using
$F.name
without preceding it with.flush()
or.close()
, as it may result in an error. This is because the file referenced by$F.name
might not exist at the time of use. To prevent issues, ensure that you either call.flush()
to write any buffered data to the file or close the file with .close() before referencing$F.name
.grit studio link: https://app.grit.io/studio?key=yeST0wXjbRx38MojeG0jn