Repository: semgrep/semgrep-rules

Issue:
python tempfile-without-flush: Don't require .write() for exception?
**Describe the bug**
The `python.lang.correctness.tempfile.flush.tempfile-without-flush` rule has [exceptions](https://github.com/returntocorp/semgrep-rules/blob/35e9f350a5d0190c936502fff179ba2c465c4438/python/lang/correctness/tempfile/flush.yaml#L5-L20) if `.write()` and then `.close()` or `.flush()` is used on a tempfile. However, this doesn't cover code which calls `.close()` immediately, without writing:

```
$ semgrep --config=p/r2c-ci --verbose test.py
[...]
severity:error rule:python.lang.correctness.tempfile.flush.tempfile-without-flush: possibly missing a .flush() or .close() call to temporary file handle; file may or may not exist when handle.name is used
3:handle = tempfile.NamedTemporaryFile(delete=False)
4:handle.close()
5:print(handle.name)
```

**To Reproduce**
```python3
import tempfile

handle = tempfile.NamedTemporaryFile(delete=False)
handle.close()
print(handle.name)
```

**Expected behavior**
It seems to me like the exception should trigger whenever `.close()` is invoked, no matter if `.write()` is called before that or not?
python tempfile-without-flush: Don't require .write() for exception?
**Describe the bug**
The `python.lang.correctness.tempfile.flush.tempfile-without-flush` rule has [exceptions](https://github.com/returntocorp/semgrep-rules/blob/35e9f350a5d0190c936502fff179ba2c465c4438/python/lang/correctness/tempfile/flush.yaml#L5-L20) if `.write()` and then `.close()` or `.flush()` is used on a tempfile. However, this doesn't cover code which calls `.close()` immediately, without writing:

```
$ semgrep --config=p/r2c-ci --verbose test.py
[...]
severity:error rule:python.lang.correctness.tempfile.flush.tempfile-without-flush: possibly missing a .flush() or .close() call to temporary file handle; file may or may not exist when handle.name is used
3:handle = tempfile.NamedTemporaryFile(delete=False)
4:handle.close()
5:print(handle.name)
```

**To Reproduce**
```python3
import tempfile

handle = tempfile.NamedTemporaryFile(delete=False)
handle.close()
print(handle.name)
```

**Expected behavior**
It seems to me like the exception should trigger whenever `.close()` is invoked, no matter if `.write()` is called before that or not?


Discussion:
Oh, nice issue number I got there :smile: 

I'd be happy to submit a PR for this if others agree this is the way to go and I'm not missing anything - taking the freedom to ping @ievans who originally added the rule in 64922d467c20e802668a8608204c62449f286b5c.
Very good point @The-Compiler, would be happy to accept a PR for that! cc @mschwager who also worked on the original rule
Oh, nice issue number I got there :smile: 

I'd be happy to submit a PR for this if others agree this is the way to go and I'm not missing anything - taking the freedom to ping @ievans who originally added the rule in 64922d467c20e802668a8608204c62449f286b5c.
Very good point @The-Compiler, would be happy to accept a PR for that! cc @mschwager who also worked on the original rule

Resolving patch:
diff --git a/python/lang/correctness/tempfile/flush.py b/python/lang/correctness/tempfile/flush.py
--- a/python/lang/correctness/tempfile/flush.py
+++ b/python/lang/correctness/tempfile/flush.py
@@ -61,3 +61,12 @@ def main_e():
     print(fout.name)
     # ruleid:tempfile-without-flush
     cmd = [binary_name, fout.name, *[str(path) for path in targets]]
+
+
+def main_f():
+    fout = tempfile.NamedTemporaryFile('w', delete=False)
+    debug_print(astr)
+    fout.close()
+
+    # ok:tempfile-without-flush
+    print(fout.name)


Test patch: