{"record":{"id":"0290b1618806b3ac","repo":"vitessio/vitess","slug":"removing-from-wrong-list","errorCode":null,"errorMessage":"removing from wrong List","messagePattern":"removing from wrong List","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/list/list.go","lineNumber":138,"sourceCode":"func (l *List[T]) move(e, at *Element[T]) {\n\tif e == at {\n\t\treturn\n\t}\n\te.prev.next = e.next\n\te.next.prev = e.prev\n\n\te.prev = at\n\te.next = at.next\n\te.prev.next = e\n\te.next.prev = e\n}\n\n// Remove removes e from l. The element must not be nil and must be an\n// element of list l; Remove panics otherwise. Use RemoveIfPresent when the\n// element may have been removed already.\nfunc (l *List[T]) Remove(e *Element[T]) {\n\tif e.list != l {\n\t\tpanic(\"removing from wrong List\")\n\t}\n\t// if e.list == l, l must have been initialized when e was inserted\n\t// in l or l == nil (e is a zero Element) and l.remove will crash\n\tl.remove(e)\n}\n\n// RemoveIfPresent removes e from l if e is currently an element of list l\n// and reports whether it did so. Unlike Remove, it is a no-op rather than a\n// panic when e is not in l, which makes membership checks O(1) for callers\n// that would otherwise have to scan the list before removing.\n// The element must not be nil.\nfunc (l *List[T]) RemoveIfPresent(e *Element[T]) bool {\n\tif e.list != l {\n\t\treturn false\n\t}\n\tl.remove(e)\n\treturn true\n}","sourceCodeStart":120,"sourceCodeEnd":156,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/list/list.go#L120-L156","documentation":"list.List.Remove panics if the given element belongs to a different list (e.list != l). Elements carry a back-pointer to their owning list; removing an element from a list it was never inserted into (or already moved to another list) is a programming error. Use RemoveIfPresent when the element may already have been removed.","triggerScenarios":"Calling l.Remove(e) where e was inserted into a different List, or where e is a zero-value Element that was never inserted, or where e was moved to another list.","commonSituations":"Managing multiple linked lists and mixing up which element came from which; removing the same element from two lists; retaining elements across list re-creation; copying an Element by value so its list pointer points at the original list.","solutions":["Ensure the element was inserted into exactly this list instance before calling Remove.","Switch to RemoveIfPresent if the element may already have been removed.","Track ownership of elements so each element is only removed from the list it came from."],"exampleFix":"// before\nif cond {\n    listA.Remove(elem) // elem actually lives in listB\n}\n// after\nif cond && elem.List() == listA {\n    listA.Remove(elem)\n}","handlingStrategy":"type-guard","validationCode":"if elem.list != mylist {\n    return errors.New(\"element does not belong to this list\")\n}\nmylist.Remove(elem)","typeGuard":"func belongsTo[T any](e *list.Element[T], l *list.List[T]) bool {\n    return e != nil && e.List() == l\n}","tryCatchPattern":"func safeRemove[T any](l *list.List[T], e *list.Element[T]) (ok bool) {\n    defer func() {\n        if r := recover(); r != nil {\n            ok = false\n        }\n    }()\n    if e.List() != l {\n        return false\n    }\n    l.Remove(e)\n    return true\n}","preventionTips":["Store a back-reference or wrapper that knows which list an element belongs to.","Prefer RemoveIfPresent when removal is conditional.","Never copy Element values; share pointers so the list back-pointer stays accurate."],"tags":["data-structure","panics","api-misuse"],"backgroundTag":"wrong-container-element","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}