From 21c947e1073c9f93649eac7566824688fc070f14 Mon Sep 17 00:00:00 2001 From: Eitol Date: Wed, 10 Jul 2024 11:40:46 -0400 Subject: [PATCH] Refactor error handling in file creation The code responsible for error handling during file creation in the desc_source.go file has been streamlined. This modification simplifies the code by reducing unnecessary condition checks and redundant file closure action after an error has occurred. --- desc_source.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/desc_source.go b/desc_source.go index fd40b27..b9c6a62 100644 --- a/desc_source.go +++ b/desc_source.go @@ -322,16 +322,14 @@ func WriteProtoFiles(outProtoDirPath string, descSource DescriptorSource, symbol filePath := filepath.Join(outFilepath, fileName) f, err := os.Create(filePath) if err != nil { - if f != nil { - _ = f.Close() - } return fmt.Errorf("failed to create file %q: %v", filePath, err) } - if err := pr.PrintProtoFile(fd, f); err != nil { + err = pr.PrintProtoFile(fd, f) + if err == nil { _ = f.Close() + } else { return fmt.Errorf("failed to write file %q: %v", filePath, err) } - _ = f.Close() } return nil }