We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN 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?

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `python/lang/correctness/tempfile/flush.py`
Content:
```
1 import tempfile
2 
3 import at
4 import tf
5 
6 
7 def main():
8     with tempfile.NamedTemporaryFile("w") as fout:
9         debug_print(astr)
10         fout.write(astr)
11         # ok:tempfile-without-flush
12         fout.flush()
13         cmd = [binary_name, fout.name, *[str(path) for path in targets]]
14 
15 
16 def main_b():
17     with tempfile.NamedTemporaryFile("w") as fout:
18         debug_print(astr)
19         fout.write(astr)
20         # ok:tempfile-without-flush
21         fout.close()
22         cmd = [binary_name, fout.name, *[str(path) for path in targets]]
23 
24 
25 def main_c():
26     with tempfile.NamedTemporaryFile("w") as fout:
27         debug_print(astr)
28         fout.write(astr)
29 
30     # ok:tempfile-without-flush
31     cmd = [binary_name, fout.name, *[str(path) for path in targets]]
32 
33 
34 def main_c():
35     with tempfile.NamedTemporaryFile("w") as fout:
36         debug_print(astr)
37         fout.write(astr)
38         debug_print('wrote file')
39 
40         # ruleid:tempfile-without-flush
41         cmd = [binary_name, fout.name, *[str(path) for path in targets]]
42 
43 
44 def main_d():
45     fout = tempfile.NamedTemporaryFile('w')
46     debug_print(astr)
47     fout.write(astr)
48 
49     # ruleid:tempfile-without-flush
50     fout.name
51     # ruleid:tempfile-without-flush
52     cmd = [binary_name, fout.name, *[str(path) for path in targets]]
53 
54 
55 def main_e():
56     fout = tempfile.NamedTemporaryFile('w')
57     debug_print(astr)
58     fout.write(astr)
59 
60     # ruleid:tempfile-without-flush
61     print(fout.name)
62     # ruleid:tempfile-without-flush
63     cmd = [binary_name, fout.name, *[str(path) for path in targets]]
64
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

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 @@
     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)

{"golden_diff": "diff --git a/python/lang/correctness/tempfile/flush.py b/python/lang/correctness/tempfile/flush.py\n--- a/python/lang/correctness/tempfile/flush.py\n+++ b/python/lang/correctness/tempfile/flush.py\n@@ -61,3 +61,12 @@\n     print(fout.name)\n     # ruleid:tempfile-without-flush\n     cmd = [binary_name, fout.name, *[str(path) for path in targets]]\n+\n+\n+def main_f():\n+    fout = tempfile.NamedTemporaryFile('w', delete=False)\n+    debug_print(astr)\n+    fout.close()\n+\n+    # ok:tempfile-without-flush\n+    print(fout.name)\n", "issue": "python tempfile-without-flush: Don't require .write() for exception?\n**Describe the bug**\r\nThe `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:\r\n\r\n```\r\n$ semgrep --config=p/r2c-ci --verbose test.py\r\n[...]\r\nseverity: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\r\n3:handle = tempfile.NamedTemporaryFile(delete=False)\r\n4:handle.close()\r\n5:print(handle.name)\r\n```\r\n\r\n**To Reproduce**\r\n```python3\r\nimport tempfile\r\n\r\nhandle = tempfile.NamedTemporaryFile(delete=False)\r\nhandle.close()\r\nprint(handle.name)\r\n```\r\n\r\n**Expected behavior**\r\nIt seems to me like the exception should trigger whenever `.close()` is invoked, no matter if `.write()` is called before that or not?\npython tempfile-without-flush: Don't require .write() for exception?\n**Describe the bug**\r\nThe `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:\r\n\r\n```\r\n$ semgrep --config=p/r2c-ci --verbose test.py\r\n[...]\r\nseverity: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\r\n3:handle = tempfile.NamedTemporaryFile(delete=False)\r\n4:handle.close()\r\n5:print(handle.name)\r\n```\r\n\r\n**To Reproduce**\r\n```python3\r\nimport tempfile\r\n\r\nhandle = tempfile.NamedTemporaryFile(delete=False)\r\nhandle.close()\r\nprint(handle.name)\r\n```\r\n\r\n**Expected behavior**\r\nIt seems to me like the exception should trigger whenever `.close()` is invoked, no matter if `.write()` is called before that or not?\n", "before_files": [{"content": "import tempfile\n\nimport at\nimport tf\n\n\ndef main():\n    with tempfile.NamedTemporaryFile(\"w\") as fout:\n        debug_print(astr)\n        fout.write(astr)\n        # ok:tempfile-without-flush\n        fout.flush()\n        cmd = [binary_name, fout.name, *[str(path) for path in targets]]\n\n\ndef main_b():\n    with tempfile.NamedTemporaryFile(\"w\") as fout:\n        debug_print(astr)\n        fout.write(astr)\n        # ok:tempfile-without-flush\n        fout.close()\n        cmd = [binary_name, fout.name, *[str(path) for path in targets]]\n\n\ndef main_c():\n    with tempfile.NamedTemporaryFile(\"w\") as fout:\n        debug_print(astr)\n        fout.write(astr)\n\n    # ok:tempfile-without-flush\n    cmd = [binary_name, fout.name, *[str(path) for path in targets]]\n\n\ndef main_c():\n    with tempfile.NamedTemporaryFile(\"w\") as fout:\n        debug_print(astr)\n        fout.write(astr)\n        debug_print('wrote file')\n\n        # ruleid:tempfile-without-flush\n        cmd = [binary_name, fout.name, *[str(path) for path in targets]]\n\n\ndef main_d():\n    fout = tempfile.NamedTemporaryFile('w')\n    debug_print(astr)\n    fout.write(astr)\n\n    # ruleid:tempfile-without-flush\n    fout.name\n    # ruleid:tempfile-without-flush\n    cmd = [binary_name, fout.name, *[str(path) for path in targets]]\n\n\ndef main_e():\n    fout = tempfile.NamedTemporaryFile('w')\n    debug_print(astr)\n    fout.write(astr)\n\n    # ruleid:tempfile-without-flush\n    print(fout.name)\n    # ruleid:tempfile-without-flush\n    cmd = [binary_name, fout.name, *[str(path) for path in targets]]\n", "path": "python/lang/correctness/tempfile/flush.py"}], "after_files": [{"content": "import tempfile\n\nimport at\nimport tf\n\n\ndef main():\n    with tempfile.NamedTemporaryFile(\"w\") as fout:\n        debug_print(astr)\n        fout.write(astr)\n        # ok:tempfile-without-flush\n        fout.flush()\n        cmd = [binary_name, fout.name, *[str(path) for path in targets]]\n\n\ndef main_b():\n    with tempfile.NamedTemporaryFile(\"w\") as fout:\n        debug_print(astr)\n        fout.write(astr)\n        # ok:tempfile-without-flush\n        fout.close()\n        cmd = [binary_name, fout.name, *[str(path) for path in targets]]\n\n\ndef main_c():\n    with tempfile.NamedTemporaryFile(\"w\") as fout:\n        debug_print(astr)\n        fout.write(astr)\n\n    # ok:tempfile-without-flush\n    cmd = [binary_name, fout.name, *[str(path) for path in targets]]\n\n\ndef main_c():\n    with tempfile.NamedTemporaryFile(\"w\") as fout:\n        debug_print(astr)\n        fout.write(astr)\n        debug_print('wrote file')\n\n        # ruleid:tempfile-without-flush\n        cmd = [binary_name, fout.name, *[str(path) for path in targets]]\n\n\ndef main_d():\n    fout = tempfile.NamedTemporaryFile('w')\n    debug_print(astr)\n    fout.write(astr)\n\n    # ruleid:tempfile-without-flush\n    fout.name\n    # ruleid:tempfile-without-flush\n    cmd = [binary_name, fout.name, *[str(path) for path in targets]]\n\n\ndef main_e():\n    fout = tempfile.NamedTemporaryFile('w')\n    debug_print(astr)\n    fout.write(astr)\n\n    # ruleid:tempfile-without-flush\n    print(fout.name)\n    # ruleid:tempfile-without-flush\n    cmd = [binary_name, fout.name, *[str(path) for path in targets]]\n\n\ndef main_f():\n    fout = tempfile.NamedTemporaryFile('w', delete=False)\n    debug_print(astr)\n    fout.close()\n\n    # ok:tempfile-without-flush\n    print(fout.name)\n", "path": "python/lang/correctness/tempfile/flush.py"}]}